
/* Version beta; July 2007 */
/* vera e. troeger and thomas pl�mper: estimating panel fixed effects models with time invariant and rarely changing explanatory variables*/
/* we thank Neal Beck, Jay Goodliffe and Rodrigo Alfaro for comments and help */


program define xtfevd, eclass byable(recall) sort
			/* you have to tsset your data before */
			/* Model: y_it = b*x_it + g*z_i + fe_i + v_it
			   z_i: time invariant in panel
			   fe_i: fixed effects
			   v_it: random error
			   X = [X];  Z = [Z]
			   X time varying, Z time invariant
			*/
	version 7
	if replay() {
		if `"`e(cmd)'"'==`"xtfevd"' {
			if _by() {
				error 190
			}
			Replay `0'
			exit `e(rc)'
		}
		error 301
	}

	syntax varlist(ts) [if] [in] [iw aw fw],	/* 
		*/ Invariant(varlist numeric ts)   	/*
		*/ [ Varying(varlist numeric ts) s2iv_endog(varlist numeric) s2iv_exog(varlist numeric)	/*
		*/ noCONstant CORC SCore(string)	/*
		*/ I(varname) 				/*
		*/ T(varname) 				/*
		*/ SKew CORR NOISy			/*
		*/ Level(integer $S_level) 		/*
		*/ Small Hascons			/*
		*/ ar1 RHOtype(string) PCSE 	 	/*
		*/ robust CLuster(varname) ITerate(integer 100) TOLerance(real 1e-6) noLOg TRace]							

	if "`invariant'" != "" & "`varying'" != "" {
		di as err "may not specify both -invariant()- and -varying()-"
		exit 198
	}

	if "`ar1'" != "" & "`constant'" != "" {
		di as err "may not specify both -ar1- and -noconstant-"
		exit 198
	}

	/* if "`ar1'" != "" & "`robust'" != "" {
		di as err "may not specify both -ar1- and -robust-"
		exit 198
	}

	if "`raremean'" != "" & "`s2iv_endog'" != "" {
		di as err "may not specify both -raremean- and -s2iv_endog-"
		exit 198
	}


	if "`raremean'" != "" & "`s2iv_exog'" != "" {
		di as err "may not specify both -raremean- and -s2iv_exog-"
		exit 198
	}*/

	if "`pcse'" != "" & "`cluster'" != "" {
		di as err "may not specify both -pcse- and -cluster-"
		exit 198
	}


	if "`robust'" != "" & "`cluster'" != "" {
		di as err "may not specify both -robust- and -cluster-"
		exit 198
	}


	if "`pcse'" != "" & "`robust'" != "" {
		di as err "may not specify both -pcse- and -robust-"
		exit 198
	}



	if "`s2iv_endog'" != "" & "`s2iv_exog'" == "" {
		di as err "you must specify both endogenous and exogneous variables for IV estimation of the second stage"
		exit 198
	}

	if "`s2iv_endog'" == "" & "`s2iv_exog'" != "" {
		di as err "you must specify both endogenous and exogneous variables for IV estimation of the second stage"
		exit 198
	}

	local typelist varlist invariant  
	local inlist : list invariant in varlist
	if !`inlist' {
		di as err "some variables specified in -invariant()- not " /*
			*/ "included in main variable list"
		exit 198
	}

	
	if "`varying'" != "" {
		local typelist `typelist' varying
		local inlist : list varying in varlist
		if !`inlist' {
			di as err "some variables specified in " /*
				*/ "-varying()- not " /*
				*/ "included in main variable list"
			exit 198
		} 
	}
	
	local fullist		/* full variable list */
	foreach type of local typelist {
		local `type' : list uniq `type'
		tsunab `type' : ``type''
		local fullist : list fullist | `type'
	}

	gettoken yvar xzlist : fullist


	local xvar : list xzlist - invariant
	local zvar `invariant'

	local ivendog `s2iv_endog'
	local ivexog `s2iv_exog'
	local iv1 : list zvar - ivendog


	local newxzlist : list xvar|zvar
	/*local testlist : list xzlist==newxzlist
		if ! `testlist' {
			di as err "SEQUENCE PROBLEM: time varying variables" /*
				*/ " have to appear before the time invariant or" /*
				*/ " rarely changing variables in the independent" /*
				*/ " variable list after the xtfevd command. AND:" /*
				*/ " time invariant and rarely changing variables" /*
				*/ " specified in the -invariant()- option must" /*
				*/ " have the same sequence as in the independent variable list" 
			exit 198
		}
	*/

	
	if "`s2iv_endog'" != "" {	
		local ivlist invariant s2iv_endog
		local inlist : list s2iv_endog in invariant
		if ! `inlist' {
			di as err "some variables specified in -s2iv_endog()- not included in invariant list"
			exit 198
		}
	}

	
	local endo_ct : word count `ivendog'
	local ex_ct : word count `ivexog'
	if `endo_ct' > `ex_ct' {
		di as err "equation not identified; must have at " /*
		*/ "least as many instruments not in"
		di as err "the regression as there are "           /*
		*/ "instrumented variables"
		exit 481
	}
	

	

	xt_iis `i'
	local ivar "`s(ivar)'"
	cap xt_tis `t'		/* to handle time-series operators */
	local tvar "`s(timevar)'"
	
	

	tempvar wt
	if "`weight'" != "" {
		qui gen double `wt' `exp'
		local wtopt "[`weight'=`wt']"
	}
	else {
		qui gen byte `wt' = 1
	}



	tempvar touse
	mark `touse' `if' `in' `wtopt'
	markout `touse' `newxzlist' 

	
	global T_corc `corc'
	global T_opts `options' `score'
	global T_score `score'
	global T_tsscon "tsscons"
	if "`hascons'" == "" { global T_cons  "_cons" }

	qui{
	
	/*
	
	local mzvar
	foreach z of local zvar {
  	local i = "_m"
  	sort `ivar'
 	tempvar `z'_tmp
  	by `ivar': egen ``z'_tmp'= mean(`z')
  	cap g `z'`i'=``z'_tmp'
  	local mzvar `mzvar' `z'`i'
	} */
		
	
	
	local mxvar
	local dmxvar
	foreach x of local xvar {
	sort `ivar'
	local i="_m"
 	local j= "_dm"
 	tempvar `x'_tmp `x'_tmp2
  	by `ivar' : egen ``x'_tmp'= mean(`x')
  	gen ``x'_tmp2'= `x'-``x'_tmp'
  	cap g `x'`i'=``x'_tmp'
  	cap g `x'`j'=``x'_tmp2'
  	local mxvar `mxvar' `x'`i'
  	local dmxvar `dmxvar' `x'`j'
		}
	}			

	qui tsset `ivar' `tvar'

	/* check colliearity */
	local nvar : word count `newxzlist'
	_rmcoll `newxzlist' `wtopt' if `touse', `constant'
	local newxzlist `"`r(varlist)'"'

	local vartype : set type
	set type double
	local names
	foreach type in xvar zvar  {
		local `type' : list `type' & newxzlist
		local names `names' ``type''
		local `type'names ``type''
		tsrevar ``type''
		local `type' `r(varlist)'
		markout `touse' ``type''
	}
	local yname `yvar'
	tsrevar `yvar'
	local yvar `r(varlist)'
	markout `touse' `yvar'
	set type `vartype'

	local ok = 1
	if "`xvar'" == "" {
		di as err "There are no time-varying exogeneous " /*
			*/ "variables in the model."
		local ok = 0
	} 
	
	if "`zvar'" == "" {
		di as err "There are no time-invariant exogeneous " /*
			*/ "variables in the model."
		local ok = 0
	} 
	
	if !`ok' {
		di as err "If you have those variables specified, " /*
			*/ "they may have been " /*
			*/ "removed due to collinearity."
		exit 198
	}

	sort `touse' `ivar'		/* _crcchkw needs to sorted by this 
					   order, but previous commands needs 
					   the order to be -by ivar tvar-
					   to handle time-series operators
					*/
				/* check fweights */
	if "`weight'" == "fweight" {
 		_crcchkw `ivar' `wt' `touse'
	}

	
	
	qui{
	tempvar m_yvar dm_yvar
	sort `ivar'
	by `ivar': egen `m_yvar' = mean(`yvar')
	gen `dm_yvar' = `yvar' - `m_yvar'

	qui tsset `ivar' `tvar'
	}	




			/* estimation*/


	qui capture drop ui eta `feres' `fe' `praisvarlist' `yvarp' `xzlistp' `ui' `eta'
		




	if "`robust'" != "" { 



	if "`ar1'" != "" {


	if "`s2iv_endog'" != "" {
		
			
			qui{
				
				xtregar `yvar' `newxzlist' if `touse', fe 
				local dffe = e(df_a)
				local fe: permname ui
				predict `fe', u
				ivreg `fe' `iv1' (`ivendog'=`ivexog') if `touse', `constant'
				local feres: permname eta 
				predict `feres', resid 
				tsset `ivar' `tvar'
				tempname b V
				tempvar ypred
				qui prais `yvar' `newxzlist' `feres' if `touse', `corc' `rhotype' `constant' nolog /*nodw*/
				predict `ypred'
				matrix `V'= e(V)
				local rows : rownames `V'
				local cols : colnames `V'
				local n_obs = e(N)
				matrix `b' = e(b)
				local ll = e(ll)
				local ic = e(ic)
				local dw = e(dw)
				local dw_o = e(dw_0)
				local rho = e(rho)
				local newvarlist `yvar' `newxzlist' `feres'
				local lvar
				local praisvarlist
				foreach x of local newvarlist {
				tsset `ivar' `tvar'
				local i="_l"
 				local j= "_co"
 				tempvar `x'_tmp `x'_tmp2
  				gen ``x'_tmp'= l.`x'
  				gen ``x'_tmp2'= `x'-`rho'*``x'_tmp'
  				cap g `x'`i'=``x'_tmp'
  				cap g `x'`j'=``x'_tmp2'
  				local lvar `lvar' `x'`i'
  				local praisvarlist `praisvarlist' `x'`j'
					}	
				tsset `ivar' `tvar'		

				
				local typelistp praisvarlist
				local fullistp		/* full variable list */
	                        foreach type of local typelistp {
		                local `type' : list uniq `type'
		                tsunab `type' : ``type''
		                local fullistp : list fullistp | `type'
	                             }

				gettoken yvarp xzlistp : fullistp
				
				
				tempvar  k n dof kdffe depvar 
				tempname D beta VMAT XX YXX Xy X hat e esqr yhat C AA BAA
				local newvarlist `yvarp' `xzlistp'
				tokenize "`newvarlist'"
				local `depvar' = `yvarp'
				
				tempvar e k n dof kdffe
				tempname D beta 
				qui reg `yvarp' `xzlistp' if `touse', `constant' mse1
				matrix `D' = e(V)
				matrix rownames `D' = `rows'
				matrix colnames `D' = `cols' 

				matrix `beta' = e(b)
				local `n' = e(N)
				local `k' = colsof(`D')
				local `dof' = ``n''-``k''+2-`ex_ct'
				local `kdffe' = ``k''+`dffe'-2+`ex_ct'
				local dfm = ``k''
				predict double `e' if `touse', residual

				matrix accum `YXX' = `newvarlist' if `touse', `constant'
				matrix `XX' = `YXX'[2...,2...]
				matrix `Xy' = `YXX'[1,2...]
				matrix `beta' = `Xy' * syminv(`XX')
				matrix `hat' = `beta' * `Xy''
				local sevarlist `yvar' `dmxvar' `zvar' `feres'
				tokenize "`sevarlist'"
				matrix accum `BAA' = `sevarlist' if `touse', `constant'
				matrix `AA' = `BAA'[2...,2...]
				matrix `C' = syminv(`AA') * (`YXX'[1,1] - `hat'[1,1]) / ``dof''
				matrix rownames `C' = `rows'
				matrix colnames `C' = `cols' 
				matrix `D' = `C'

				replace `touse'= e(sample)
			
				estimates post `b' `D', dof (``dof'') esample (`touse') depname("`yvar'") obs(``n'')
 				_robust `e' if e(sample), minus(``kdffe'')
				estimates matrix DM `C'
				estimates matrix beta `beta'
				estimates local vcetype fevd_robust
				estimates local depvar `yvarp'
				estimates local cmd xtfevd
				est local invariant `zvar'
				est scalar df_fevd = ``dof''
				est scalar df_fe = `dffe'
				est scalar df_m = `dfm'
				est scalar rho = `rho'
				est scalar ll = `ll'
				est scalar ic = `ic'
				est scalar dw_t = `dw'
				est scalar dw_o = `dw_o'
				*est scalar N = `n_obs'
				}

				
				qui{	
					
					tempvar  error errs hats myhat
					
					gen `error' = `yvar'- `ypred'
					gen `errs' = `error'*`error'
					sum `errs', meanonly
					local mse1 = r(mean)
					local rss1 = r(sum)
					est scalar mse = r(mean)
					est scalar rss= r(sum)
					est scalar rmse = sqrt(r(mean))
					egen `myhat' = mean(`ypred')
					gen `hats' = (`ypred'-`myhat')*(`ypred'-`myhat')
					sum `hats', meanonly
					local ess1 = r(sum)
					est scalar ess= r(sum)
					
					local tss1=`rss1'+`ess1'
					est scalar tss=`rss1'+`ess1'
					est scalar r2 = `ess1' /`tss1'
					est scalar r2_a = 1-((`rss1'/``dof'')/(`tss1'/(``n''-1)))

					

					
					local testvarlist `newxzlist' `feres' 
					test `testvarlist'
					
					est scalar F = r(F)
					est scalar F_p = r(p)
				
					}
				
				

			
				
				di
				di in gr "panel fixed effects regression with vector decomposition, robust standard errors, ar1 prais-winsten transformation, 2nd step IV estimation"
				di
				di in gr "degrees of freedom fevd" _col(28) "=" in ye %9.0g e(df_fevd) _col(49) in gr "number of obs" _col(69) "=" in ye %9.0g e(N)
				di in gr "mean squared error" _col(28) "=" in ye %9.0g e(mse)  _col(49) in gr "F( " in ye e(df_m) in gr ", " in ye e(df_fevd) in gr ")" _col(69) "=" in ye %9.0g e(F)
				di in gr "root mean squared error" _col(28) "=" in ye %9.0g e(rmse) _col(49) in gr "Prob > F" _col(69) "=" in ye %9.0g e(F_p)	
				di in gr "Residual Sum of Squares" _col(28) "=" in ye %9.0g e(rss) _col(49) in gr "R-squared" _col(69) "=" in ye %9.0g e(r2)
				di in gr "Total Sum of Squares" _col(28) "=" in ye %9.0g e(tss) _col(49) in gr "adj. R-squared" _col(69) "=" in ye %9.0g e(r2_a)
				di in gr "Estimation Sum of Squares" _col(28) "=" in ye %9.0g e(ess)
				di
				estimates display
				di in gr "final rho after iterations" _col(40) "= " in ye %9.0g e(rho)
				di in gr "log likelihood" _col(40) "= " in ye %9.0g e(ll)
				di in gr "number of iterations" _col(40) "= " in ye %9.0g e(ic)
				di in gr "Durbin-Watson statistic (original)"  _col(40) "= " in ye %9.0g e(dw_o)
				di in gr "Durbin-Watson statistic (transformed)"  _col(40) "= " in ye %9.0g e(dw_t)


		if "`skew'" != "" {   
				di
				di in gr "skewness and normality test for the fixed effects of the first stage and the error term of the second stage
				di
				normtest `fe' `feres'
				}
	
		if "`corr'" != "" {
				di
				di in gr "correlation of fixed effects with explanatory variables"
				di
				corr `fe' `feres' `fullist'
				}
		else { di }

			

			
		}




		else {
		
			qui{
		
				
				xtregar `yvar' `newxzlist' if `touse', fe 
				local dffe = e(df_a)
				local fe: permname ui
				predict `fe', u
				regress `fe' `zvar' if `touse', `constant' 
				local feres: permname eta 
				predict `feres', resid 
				tsset `ivar' `tvar'
				tempname b V
				tempvar ypred
				qui prais `yvar' `newxzlist' `feres' if `touse', `corc' `rhotype' `constant' nolog /*nodw*/
				predict `ypred'
				matrix `V'= e(V)
				local rows : rownames `V'
				local cols : colnames `V'
				local n_obs = e(N)
				matrix `b' = e(b)
				local ll = e(ll)
				local ic = e(ic)
				local dw = e(dw)
				local dw_o = e(dw_0)
				local rho = e(rho)
				local newvarlist `yvar' `newxzlist' `feres'
				local lvar
				local praisvarlist
				foreach x of local newvarlist {
				tsset `ivar' `tvar'
				local i="_l"
 				local j= "_co"
 				tempvar `x'_tmp `x'_tmp2
  				gen ``x'_tmp'= l.`x'
  				gen ``x'_tmp2'= `x'-`rho'*``x'_tmp'
  				cap g `x'`i'=``x'_tmp'
  				cap g `x'`j'=``x'_tmp2'
  				local lvar `lvar' `x'`i'
  				local praisvarlist `praisvarlist' `x'`j'
					}	
				tsset `ivar' `tvar'		

				
				local typelistp praisvarlist
				local fullistp		/* full variable list */
	                        foreach type of local typelistp {
		                local `type' : list uniq `type'
		                tsunab `type' : ``type''
		                local fullistp : list fullistp | `type'
	                             }

				gettoken yvarp xzlistp : fullistp
				
				
				tempvar  k n dof kdffe depvar 
				tempname D beta VMAT XX YXX Xy X hat e esqr yhat C AA BAA
				local newvarlist `yvarp' `xzlistp'
				tokenize "`newvarlist'"
				local `depvar' = `yvarp'
				
				tempvar e k n dof kdffe
				tempname D beta 
				qui reg `yvarp' `xzlistp' if `touse', `constant' mse1
				matrix `D' = e(V)
				matrix rownames `D' = `rows'
				matrix colnames `D' = `cols' 

				matrix `beta' = e(b)
				local `n' = e(N)
				local `k' = colsof(`D')
				local `dof' = ``n''-``k''+2
				local `kdffe' = ``k''+`dffe'-2
				local dfm = ``k''
				predict double `e' if `touse', residual

				matrix accum `YXX' = `newvarlist' if `touse', `constant'
				matrix `XX' = `YXX'[2...,2...]
				matrix `Xy' = `YXX'[1,2...]
				matrix `beta' = `Xy' * syminv(`XX')
				matrix `hat' = `beta' * `Xy''
				local sevarlist `yvar' `dmxvar' `zvar' `feres'
				tokenize "`sevarlist'"
				matrix accum `BAA' = `sevarlist' if `touse', `constant'
				matrix `AA' = `BAA'[2...,2...]
				matrix `C' = syminv(`AA') * (`YXX'[1,1] - `hat'[1,1]) / ``dof''
				matrix rownames `C' = `rows'
				matrix colnames `C' = `cols' 
				matrix `D' = `C'

				replace `touse'= e(sample)
			
				estimates post `b' `D', dof (``dof'') esample (`touse') depname("`yvar'") obs(``n'')
 				_robust `e' if e(sample), minus(``kdffe'')
				estimates matrix DM `C'
				estimates matrix beta `beta'
				estimates local vcetype fevd_robust
				estimates local depvar `yvarp'
				estimates local cmd xtfevd
				est local invariant `zvar'
				est scalar df_fevd = ``dof''
				est scalar df_fe = `dffe'
				est scalar df_m = `dfm'
				est scalar rho = `rho'
				est scalar ll = `ll'
				est scalar ic = `ic'
				est scalar dw_t = `dw'
				est scalar dw_o = `dw_o'
				*est scalar N = `n_obs'
				}

				
				qui{	
					
					tempvar  error errs hats myhat
					
					gen `error' = `yvar'- `ypred'
					gen `errs' = `error'*`error'
					sum `errs', meanonly
					local mse1 = r(mean)
					local rss1 = r(sum)
					est scalar mse = r(mean)
					est scalar rss= r(sum)
					est scalar rmse = sqrt(r(mean))
					egen `myhat' = mean(`ypred')
					gen `hats' = (`ypred'-`myhat')*(`ypred'-`myhat')
					sum `hats', meanonly
					local ess1 = r(sum)
					est scalar ess= r(sum)
					
					local tss1=`rss1'+`ess1'
					est scalar tss=`rss1'+`ess1'
					est scalar r2 = `ess1' /`tss1'
					est scalar r2_a = 1-((`rss1'/``dof'')/(`tss1'/(``n''-1)))

					

					
					local testvarlist `newxzlist' `feres' 
					test `testvarlist'
					
					est scalar F = r(F)
					est scalar F_p = r(p)
				
					}
				
				

			
				
				di
				di in gr "panel fixed effects regression with vector decomposition, robust standard errors and ar1 prais-winsten transformation"
				di
				di in gr "degrees of freedom fevd" _col(28) "=" in ye %9.0g e(df_fevd) _col(49) in gr "number of obs" _col(69) "=" in ye %9.0g e(N)
				di in gr "mean squared error" _col(28) "=" in ye %9.0g e(mse)  _col(49) in gr "F( " in ye e(df_m) in gr ", " in ye e(df_fevd) in gr ")" _col(69) "=" in ye %9.0g e(F)
				di in gr "root mean squared error" _col(28) "=" in ye %9.0g e(rmse) _col(49) in gr "Prob > F" _col(69) "=" in ye %9.0g e(F_p)	
				di in gr "Residual Sum of Squares" _col(28) "=" in ye %9.0g e(rss) _col(49) in gr "R-squared" _col(69) "=" in ye %9.0g e(r2)
				di in gr "Total Sum of Squares" _col(28) "=" in ye %9.0g e(tss) _col(49) in gr "adj. R-squared" _col(69) "=" in ye %9.0g e(r2_a)
				di in gr "Estimation Sum of Squares" _col(28) "=" in ye %9.0g e(ess)
				di
				estimates display
				di in gr "final rho after iterations" _col(40) "= " in ye %9.0g e(rho)
				di in gr "log likelihood" _col(40) "= " in ye %9.0g e(ll)
				di in gr "number of iterations" _col(40) "= " in ye %9.0g e(ic)
				di in gr "Durbin-Watson statistic (original)"  _col(40) "= " in ye %9.0g e(dw_o)
				di in gr "Durbin-Watson statistic (transformed)"  _col(40) "= " in ye %9.0g e(dw_t)


		if "`skew'" != "" {   
				di
				di in gr "skewness and normality test for the fixed effects of the first stage and the error term of the second stage
				di
				normtest `fe' `feres'
				}
	
		if "`corr'" != "" {
				di
				di in gr "correlation of fixed effects with explanatory variables"
				di
				corr `fe' `feres' `fullist'
				}
		else { di }

			
			}
			
		}


	else if "`s2iv_endog'" != "" {

			
			qui{
				xtreg `yvar' `newxzlist' if `touse', fe 
				local dffe = e(df_a)
				local fe: permname ui
				predict `fe', u
				ivreg `fe' `iv1' (`ivendog'=`ivexog') if `touse', `constant' 
				local feres: permname eta
				predict `feres', resid 
				tsset `ivar' `tvar'
				tempvar e k n dof kdffe
				tempname D b 
				reg `yvar' `newxzlist' `feres' if `touse', `constant' mse1
				matrix `D' = e(V)
				local rows : rownames `D'
				local cols : colnames `D' 
				matrix `b' = e(b)
				local `n' = e(N)
				local `k' = colsof(`D')
				local `dof' = ``n''-``k''-`dffe'+1-`ex_ct'
				local `kdffe' = ``k''+`dffe'-2+`ex_ct'
				local dfm = ``k''
				predict double `e' if `touse', residual

				tempvar  depvar 
				tempname b V XX YXX Xy X hat C BAA AA
				local newvarlist `yvar' `newxzlist' `feres'
				tokenize "`newvarlist'"
				local `depvar' = `yvar'
				matrix accum `YXX' = `newvarlist' if `touse', `constant'
				matrix `XX' = `YXX'[2...,2...]
				matrix `Xy' = `YXX'[1,2...]
				matrix `b' = `Xy' * syminv(`XX')
				matrix `hat' = `b' * `Xy''
				local sevarlist `yvar' `dmxvar' `zvar' `feres'
				tokenize "`sevarlist'"
				matrix accum `BAA' = `sevarlist' if `touse', `constant'
				matrix `AA' = `BAA'[2...,2...]
				matrix `C' = syminv(`AA') * (`YXX'[1,1] - `hat'[1,1]) / ``dof''
				matrix rownames `C' = `rows'
				matrix colnames `C' = `cols'
				matrix `D' = `C'

				replace `touse'= e(sample)
			
				estimates post `b' `D', dof (``dof'') esample (`touse') depname("`yvar'") obs(``n'')
 				_robust `e' if e(sample), minus(``kdffe'')
				estimates matrix DM `C'
				estimates local vcetype "fevd_robust"
				estimates local depvar "`yvar'"
				estimates local cmd "xtfevd"
				est local invariant `zvar'
				est scalar df_fevd = ``dof''
				est scalar df_fe = `dffe'
				est scalar df_m = `dfm'
				}
				
				qui{	
					
					tempvar  error errs hats myhat yhat
					predict `yhat'
					gen `error' = `yvar'- `yhat'
					gen `errs' = `error'*`error'
					sum `errs', meanonly
					local mse1 = r(mean)
					local rss1 = r(sum)
					est scalar mse = r(mean)
					est scalar rss= r(sum)
					est scalar rmse = sqrt(r(mean))
					egen `myhat' = mean(`yhat')
					gen `hats' = (`yhat'-`myhat')*(`yhat'-`myhat')
					sum `hats', meanonly
					local ess1 = r(sum)
					est scalar ess= r(sum)
					
					local tss1=`rss1'+`ess1'
					est scalar tss=`rss1'+`ess1'
					est scalar r2 = `ess1' /`tss1'
					est scalar r2_a = 1-((`rss1'/``dof'')/(`tss1'/(``n''-1)))

					

					
					local testvarlist `newxzlist' `feres'
					test `testvarlist'
					
					est scalar F = r(F)
					est scalar F_p = r(p)
				
					}

			
				
				di
				di in gr "panel fixed effects regression with vector decomposition, robust standard errors,2nd step IV estimation"
				di
				di in gr "degrees of freedom fevd" _col(28) "=" in ye %9.0g e(df_fevd) _col(49) in gr "number of obs" _col(69) "=" in ye %9.0g e(N)
				di in gr "mean squared error" _col(28) "=" in ye %9.0g e(mse)  _col(49) in gr "F( " in ye e(df_m) in gr ", " in ye e(df_fevd) in gr ")" _col(69) "=" in ye %9.0g e(F)
				di in gr "root mean squared error" _col(28) "=" in ye %9.0g e(rmse) _col(49) in gr "Prob > F" _col(69) "=" in ye %9.0g e(F_p)	
				di in gr "Residual Sum of Squares" _col(28) "=" in ye %9.0g e(rss) _col(49) in gr "R-squared" _col(69) "=" in ye %9.0g e(r2)
				di in gr "Total Sum of Squares" _col(28) "=" in ye %9.0g e(tss) _col(49) in gr "adj. R-squared" _col(69) "=" in ye %9.0g e(r2_a)
				di in gr "Estimation Sum of Squares" _col(28) "=" in ye %9.0g e(ess)
				di
				estimates display

				
				

		if "`skew'" != "" {   
				di
				di in gr "skewness and normality test for the fixed effects of the first stage and the error term of the second stage
				di
				normtest `fe' `feres'
				}
	
		else if "`corr'" != "" {
				di
				di in gr "correlation of fixed effects with explanatory variables"
				di
				corr `fe' `feres' `fullist'
				}
		else { di }
			

		}




	else {

			
			qui{
				xtreg `yvar' `newxzlist' if `touse', fe 
				local dffe = e(df_a)
				local fe: permname ui
				predict `fe', u
				regress `fe' `zvar' if `touse', `constant'
				local feres: permname eta
				predict `feres', resid 
				tsset `ivar' `tvar'
				tempvar e k n dof kdffe
				tempname D b 
				reg `yvar' `newxzlist' `feres' if `touse', `constant' mse1
				matrix `D' = e(V)
				local rows : rownames `D'
				local cols : colnames `D' 
				matrix `b' = e(b)
				local `n' = e(N)
				local `k' = colsof(`D')
				local `dof' = ``n''-``k''-`dffe'+1
				local `kdffe' = ``k''+`dffe'-2
				local dfm = ``k''
				predict double `e' if `touse', residual

				tempvar  depvar 
				tempname b V XX YXX Xy X hat C BAA AA
				local newvarlist `yvar' `newxzlist' `feres'
				tokenize "`newvarlist'"
				local `depvar' = `yvar'
				matrix accum `YXX' = `newvarlist' if `touse', `constant'
				matrix `XX' = `YXX'[2...,2...]
				matrix `Xy' = `YXX'[1,2...]
				matrix `b' = `Xy' * syminv(`XX')
				matrix `hat' = `b' * `Xy''
				local sevarlist `yvar' `dmxvar' `zvar' `feres'
				tokenize "`sevarlist'"
				matrix accum `BAA' = `sevarlist' if `touse', `constant'
				matrix `AA' = `BAA'[2...,2...]
				matrix `C' = syminv(`AA') * (`YXX'[1,1] - `hat'[1,1]) / ``dof''
				matrix rownames `C' = `rows'
				matrix colnames `C' = `cols'
				matrix `D' = `C'

				replace `touse'= e(sample)
				
				estimates post `b' `D', dof (``dof'') esample (`touse') depname("`yvar'") obs(``n'')
 				_robust `e' if e(sample), minus(``kdffe'') 
				estimates matrix DM `C'
				estimates local vcetype "fevd_robust"
				estimates local depvar "`yvar'"
				estimates local cmd "xtfevd"
				est local invariant `zvar'
				est scalar df_fevd = ``dof''
				est scalar df_fe = `dffe'
				est scalar df_m = `dfm'
				}
				
				qui{	
					
					tempvar  error errs hats myhat yhat
					predict `yhat'
					gen `error' = `yvar'- `yhat'
					gen `errs' = `error'*`error'
					sum `errs', meanonly
					local mse1 = r(mean)
					local rss1 = r(sum)
					est scalar mse = r(mean)
					est scalar rss= r(sum)
					est scalar rmse = sqrt(r(mean))
					egen `myhat' = mean(`yhat')
					gen `hats' = (`yhat'-`myhat')*(`yhat'-`myhat')
					sum `hats', meanonly
					local ess1 = r(sum)
					est scalar ess= r(sum)
					
					local tss1=`rss1'+`ess1'
					est scalar tss=`rss1'+`ess1'
					est scalar r2 = `ess1' /`tss1'
					est scalar r2_a = 1-((`rss1'/``dof'')/(`tss1'/(``n''-1)))

					

					
					local testvarlist `newxzlist' `feres'
					test `testvarlist'
					
					est scalar F = r(F)
					est scalar F_p = r(p)
				
					}

			
				
				di
				di in gr "panel fixed effects regression with vector decomposition, robust standard errors"
				di
				di in gr "degrees of freedom fevd" _col(28) "=" in ye %9.0g e(df_fevd) _col(49) in gr "number of obs" _col(69) "=" in ye %9.0g e(N)
				di in gr "mean squared error" _col(28) "=" in ye %9.0g e(mse)  _col(49) in gr "F( " in ye e(df_m) in gr ", " in ye e(df_fevd) in gr ")" _col(69) "=" in ye %9.0g e(F)
				di in gr "root mean squared error" _col(28) "=" in ye %9.0g e(rmse) _col(49) in gr "Prob > F" _col(69) "=" in ye %9.0g e(F_p)	
				di in gr "Residual Sum of Squares" _col(28) "=" in ye %9.0g e(rss) _col(49) in gr "R-squared" _col(69) "=" in ye %9.0g e(r2)
				di in gr "Total Sum of Squares" _col(28) "=" in ye %9.0g e(tss) _col(49) in gr "adj. R-squared" _col(69) "=" in ye %9.0g e(r2_a)
				di in gr "Estimation Sum of Squares" _col(28) "=" in ye %9.0g e(ess)
				di
				estimates display

				
				

		if "`skew'" != "" {   
				di
				di in gr "skewness and normality test for the fixed effects of the first stage and the error term of the second stage
				di
				normtest `fe' `feres'
				}
	
		else if "`corr'" != "" {
				di
				di in gr "correlation of fixed effects with explanatory variables"
				di
				corr `fe' `feres' `fullist'
				}
		else { di }
			

		}

	}


	else if "`cluster'" != "" { 



	if "`ar1'" != "" {


	if "`s2iv_endog'" != "" {
		
			
			qui{
				
				xtregar `yvar' `newxzlist' if `touse', fe 
				local dffe = e(df_a)
				local fe: permname ui
				predict `fe', u
				ivreg `fe' `iv1' (`ivendog'=`ivexog') if `touse', `constant' 
				local feres: permname eta 
				predict `feres', resid 
				tsset `ivar' `tvar'
				tempname b V
				tempvar ypred
				qui prais `yvar' `newxzlist' `feres' if `touse', `corc' `rhotype' `constant' nolog /*nodw*/
				predict `ypred'
				matrix `V'= e(V)
				local rows : rownames `V'
				local cols : colnames `V'
				local n_obs = e(N)
				matrix `b' = e(b)
				local ll = e(ll)
				local ic = e(ic)
				local dw = e(dw)
				local dw_o = e(dw_0)
				local rho = e(rho)
				local newvarlist `yvar' `newxzlist' `feres'
				local lvar
				local praisvarlist
				foreach x of local newvarlist {
				tsset `ivar' `tvar'
				local i="_l"
 				local j= "_co"
 				tempvar `x'_tmp `x'_tmp2
  				gen ``x'_tmp'= l.`x'
  				gen ``x'_tmp2'= `x'-`rho'*``x'_tmp'
  				cap g `x'`i'=``x'_tmp'
  				cap g `x'`j'=``x'_tmp2'
  				local lvar `lvar' `x'`i'
  				local praisvarlist `praisvarlist' `x'`j'
					}	
				tsset `ivar' `tvar'		

				
				local typelistp praisvarlist
				local fullistp		/* full variable list */
	                        foreach type of local typelistp {
		                local `type' : list uniq `type'
		                tsunab `type' : ``type''
		                local fullistp : list fullistp | `type'
	                             }

				gettoken yvarp xzlistp : fullistp

				tempvar  k n dof kdffe depvar 
				tempname D beta VMAT XX YXX Xy X hat e esqr yhat C BAA AA

				local newvarlist `yvarp' `xzlistp'
				tokenize "`newvarlist'"
				local `depvar' = `yvarp'
				
				tempvar e k n dof kdffe
				tempvar count sedof
				tempname D  
				qui reg `yvarp' `xzlistp' if `touse', `constant' mse1
				matrix `D' = e(V)
				matrix rownames `D' = `rows'
				matrix colnames `D' = `cols' 
				matrix `beta' = e(b)
				local `n' = e(N)
				local `k' = colsof(`D')
				sort `touse' `cluster'
				by `touse' `cluster': gen byte `count' = 1 if _n==1  & `touse'
				summarize `count', meanonly
				local nclust = r(sum)
				local `sedof' = `nclust' - 1
				local clopt "cluster(`cluster')"
				local `dof' = ``n''-``k''+2-`ex_ct'
				local `kdffe' = ``k''+`dffe'-2+`ex_ct'
				local dfm = ``k''
				predict double `e' if `touse', residual


				matrix accum `YXX' = `newvarlist' if `touse', `constant'
				matrix `XX' = `YXX'[2...,2...]
				matrix `Xy' = `YXX'[1,2...]
				matrix `beta' = `Xy' * syminv(`XX')
				matrix `hat' = `beta' * `Xy''
				local sevarlist `yvar' `dmxvar' `zvar' `feres'
				tokenize "`sevarlist'"
				matrix accum `BAA' = `sevarlist' if `touse', `constant'
				matrix `AA' = `BAA'[2...,2...]
				matrix `C' = syminv(`AA') * (`YXX'[1,1] - `hat'[1,1]) / ``dof''
				matrix rownames `C' = `rows'
				matrix colnames `C' = `cols' 
				matrix `D'= `C'

				replace `touse'= e(sample)
				estimates post `b' `D', dof (``dof'') esample (`touse') depname("`yvar'") obs(``n'')
				_robust `e' if e(sample), minus(``kdffe'') `clopt'
				estimates matrix DM `C'
				estimates matrix beta `beta'
				estimates local vcetype "fevd clustered"
				estimates local depvar `yvar'
				estimates local cmd xtfevd
				est local invariant `zvar'
				est scalar df_fevd = ``dof''
				est scalar df_fe = `dffe'
				est scalar df_m = `dfm'
				est scalar rho = `rho'
				est scalar ll = `ll'
				est scalar ic = `ic'
				est scalar dw_t = `dw'
				est scalar dw_o = `dw_o'
				*est scalar N = `n_obs'
				}

				
				qui{	
					
					tempvar  error errs hats myhat
					
					gen `error' = `yvar'- `ypred'
					gen `errs' = `error'*`error'
					sum `errs', meanonly
					local mse1 = r(mean)
					local rss1 = r(sum)
					est scalar mse = r(mean)
					est scalar rss= r(sum)
					est scalar rmse = sqrt(r(mean))
					egen `myhat' = mean(`ypred')
					gen `hats' = (`ypred'-`myhat')*(`ypred'-`myhat')
					sum `hats', meanonly
					local ess1 = r(sum)
					est scalar ess= r(sum)
					
					local tss1=`rss1'+`ess1'
					est scalar tss=`rss1'+`ess1'
					est scalar r2 = `ess1' /`tss1'
					est scalar r2_a = 1-((`rss1'/``dof'')/(`tss1'/(``n''-1)))

					

					
					local testvarlist `newxzlist' `feres' 
					test `testvarlist'
					
					est scalar F = r(F)
					est scalar F_p = r(p)
				
					}
				
				

			
				
				di
				di in gr "panel fixed effects regression with vector decomposition, cluster, 2nd step IV estimation, and ar1 prais-winsten transformation"
				di
				di in gr "degrees of freedom fevd" _col(28) "=" in ye %9.0g e(df_fevd) _col(49) in gr "number of obs" _col(69) "=" in ye %9.0g e(N)
				di in gr "mean squared error" _col(28) "=" in ye %9.0g e(mse)  _col(49) in gr "F( " in ye e(df_m) in gr ", " in ye e(df_fevd) in gr ")" _col(69) "=" in ye %9.0g e(F)
				di in gr "root mean squared error" _col(28) "=" in ye %9.0g e(rmse) _col(49) in gr "Prob > F" _col(69) "=" in ye %9.0g e(F_p)	
				di in gr "Residual Sum of Squares" _col(28) "=" in ye %9.0g e(rss) _col(49) in gr "R-squared" _col(69) "=" in ye %9.0g e(r2)
				di in gr "Total Sum of Squares" _col(28) "=" in ye %9.0g e(tss) _col(49) in gr "adj. R-squared" _col(69) "=" in ye %9.0g e(r2_a)
				di in gr "Estimation Sum of Squares" _col(28) "=" in ye %9.0g e(ess)
				di
				estimates display
				di in gr "final rho after iterations" _col(40) "= " in ye %9.0g e(rho)
				di in gr "log likelihood" _col(40) "= " in ye %9.0g e(ll)
				di in gr "number of iterations" _col(40) "= " in ye %9.0g e(ic)
				di in gr "Durbin-Watson statistic (original)"  _col(40) "= " in ye %9.0g e(dw_o)
				di in gr "Durbin-Watson statistic (transformed)"  _col(40) "= " in ye %9.0g e(dw_t)


		if "`skew'" != "" {   
				di
				di in gr "skewness and normality test for the fixed effects of the first stage and the error term of the second stage
				di
				normtest `fe' `feres'
				}
	
		if "`corr'" != "" {
				di
				di in gr "correlation of fixed effects with explanatory variables"
				di
				corr `fe' `feres' `fullist'
				}
		else { di }

			

			
		}




		else {
		
			
			qui{
				
				xtregar `yvar' `newxzlist' if `touse', fe 
				local dffe = e(df_a)
				local fe: permname ui
				predict `fe', u
				regress `fe' `zvar' if `touse', `constant' 
				local feres: permname eta 
				predict `feres', resid 
				tsset `ivar' `tvar'
				tempname b V
				tempvar ypred
				qui prais `yvar' `newxzlist' `feres' if `touse', `corc' `rhotype' `constant' nolog /*nodw*/
				predict `ypred'
				matrix `V'= e(V)
				local rows : rownames `V'
				local cols : colnames `V'
				local n_obs = e(N)
				matrix `b' = e(b)
				local ll = e(ll)
				local ic = e(ic)
				local dw = e(dw)
				local dw_o = e(dw_0)
				local rho = e(rho)
				local newvarlist `yvar' `newxzlist' `feres'
				local lvar
				local praisvarlist
				foreach x of local newvarlist {
				tsset `ivar' `tvar'
				local i="_l"
 				local j= "_co"
 				tempvar `x'_tmp `x'_tmp2
  				gen ``x'_tmp'= l.`x'
  				gen ``x'_tmp2'= `x'-`rho'*``x'_tmp'
  				cap g `x'`i'=``x'_tmp'
  				cap g `x'`j'=``x'_tmp2'
  				local lvar `lvar' `x'`i'
  				local praisvarlist `praisvarlist' `x'`j'
					}	
				tsset `ivar' `tvar'		

				
				local typelistp praisvarlist
				local fullistp		/* full variable list */
	                        foreach type of local typelistp {
		                local `type' : list uniq `type'
		                tsunab `type' : ``type''
		                local fullistp : list fullistp | `type'
	                             }

				gettoken yvarp xzlistp : fullistp

				tempvar  k n dof kdffe depvar 
				tempname D beta VMAT XX YXX Xy X hat e esqr yhat C BAA AA

				local newvarlist `yvarp' `xzlistp'
				tokenize "`newvarlist'"
				local `depvar' = `yvarp'
				
				tempvar e k n dof kdffe
				tempvar count sedof
				tempname D  
				qui reg `yvarp' `xzlistp' if `touse', `constant' mse1
				matrix `D' = e(V)
				matrix rownames `D' = `rows'
				matrix colnames `D' = `cols' 
				matrix `beta' = e(b)
				local `n' = e(N)
				local `k' = colsof(`D')
				sort `touse' `cluster'
				by `touse' `cluster': gen byte `count' = 1 if _n==1  & `touse'
				summarize `count', meanonly
				local nclust = r(sum)
				local `sedof' = `nclust' - 1
				local clopt "cluster(`cluster')"
				local `dof' = ``n''-``k''+2
				local `kdffe' = ``k''+`dffe'-2
				local dfm = ``k''
				predict double `e' if `touse', residual


				matrix accum `YXX' = `newvarlist' if `touse', `constant'
				matrix `XX' = `YXX'[2...,2...]
				matrix `Xy' = `YXX'[1,2...]
				matrix `beta' = `Xy' * syminv(`XX')
				matrix `hat' = `beta' * `Xy''
				local sevarlist `yvar' `dmxvar' `zvar' `feres'
				tokenize "`sevarlist'"
				matrix accum `BAA' = `sevarlist' if `touse', `constant'
				matrix `AA' = `BAA'[2...,2...]
				matrix `C' = syminv(`AA') * (`YXX'[1,1] - `hat'[1,1]) / ``dof''
				matrix rownames `C' = `rows'
				matrix colnames `C' = `cols' 
				matrix `D' = `C'

				replace `touse'= e(sample)
				estimates post `b' `D', dof (``dof'') esample (`touse') depname("`yvar'") obs(``n'')
				_robust `e' if e(sample), minus(``kdffe'') `clopt'
				estimates matrix DM `C'
				estimates matrix beta `beta'
				estimates local vcetype "fevd clustered"
				estimates local depvar `yvar'
				estimates local cmd xtfevd
				est local invariant `zvar'
				est scalar df_fevd = ``dof''
				est scalar df_fe = `dffe'
				est scalar df_m = `dfm'
				est scalar rho = `rho'
				est scalar ll = `ll'
				est scalar ic = `ic'
				est scalar dw_t = `dw'
				est scalar dw_o = `dw_o'
				*est scalar N = `n_obs'
				}

				
				qui{	
					
					tempvar  error errs hats myhat
					
					gen `error' = `yvar'- `ypred'
					gen `errs' = `error'*`error'
					sum `errs', meanonly
					local mse1 = r(mean)
					local rss1 = r(sum)
					est scalar mse = r(mean)
					est scalar rss= r(sum)
					est scalar rmse = sqrt(r(mean))
					egen `myhat' = mean(`ypred')
					gen `hats' = (`ypred'-`myhat')*(`ypred'-`myhat')
					sum `hats', meanonly
					local ess1 = r(sum)
					est scalar ess= r(sum)
					
					local tss1=`rss1'+`ess1'
					est scalar tss=`rss1'+`ess1'
					est scalar r2 = `ess1' /`tss1'
					est scalar r2_a = 1-((`rss1'/``dof'')/(`tss1'/(``n''-1)))

					

					
					local testvarlist `newxzlist' `feres' 
					test `testvarlist'
					
					est scalar F = r(F)
					est scalar F_p = r(p)
				
					}
				
				

			
				
				di
				di in gr "panel fixed effects regression with vector decomposition, cluster, and ar1 prais-winsten transformation"
				di
				di in gr "degrees of freedom fevd" _col(28) "=" in ye %9.0g e(df_fevd) _col(49) in gr "number of obs" _col(69) "=" in ye %9.0g e(N)
				di in gr "mean squared error" _col(28) "=" in ye %9.0g e(mse)  _col(49) in gr "F( " in ye e(df_m) in gr ", " in ye e(df_fevd) in gr ")" _col(69) "=" in ye %9.0g e(F)
				di in gr "root mean squared error" _col(28) "=" in ye %9.0g e(rmse) _col(49) in gr "Prob > F" _col(69) "=" in ye %9.0g e(F_p)	
				di in gr "Residual Sum of Squares" _col(28) "=" in ye %9.0g e(rss) _col(49) in gr "R-squared" _col(69) "=" in ye %9.0g e(r2)
				di in gr "Total Sum of Squares" _col(28) "=" in ye %9.0g e(tss) _col(49) in gr "adj. R-squared" _col(69) "=" in ye %9.0g e(r2_a)
				di in gr "Estimation Sum of Squares" _col(28) "=" in ye %9.0g e(ess)
				di
				estimates display
				di in gr "final rho after iterations" _col(40) "= " in ye %9.0g e(rho)
				di in gr "log likelihood" _col(40) "= " in ye %9.0g e(ll)
				di in gr "number of iterations" _col(40) "= " in ye %9.0g e(ic)
				di in gr "Durbin-Watson statistic (original)"  _col(40) "= " in ye %9.0g e(dw_o)
				di in gr "Durbin-Watson statistic (transformed)"  _col(40) "= " in ye %9.0g e(dw_t)


		if "`skew'" != "" {   
				di
				di in gr "skewness and normality test for the fixed effects of the first stage and the error term of the second stage
				di
				normtest `fe' `feres'
				}
	
		if "`corr'" != "" {
				di
				di in gr "correlation of fixed effects with explanatory variables"
				di
				corr `fe' `feres' `fullist'
				}
		else { di }

			}

			
		}



	else if "`s2iv_endog'" != "" {

			qui {
			
				xtreg `yvar' `newxzlist' if `touse', fe
				local dffe = e(df_a)
				local fe: permname ui
				predict `fe', u
				ivreg `fe' `iv1' (`ivendog'=`ivexog') if `touse', `constant' 
				local feres: permname eta
				predict `feres', resid 
				tsset `ivar' `tvar'
				tempvar e k n dof kdffe
				tempvar count sedof
				tempname D b 
				reg `yvar' `newxzlist' `feres' if `touse', `constant' mse1
				matrix `D' = e(V)
				local rows : rownames `D'
				local cols : colnames `D' 
				matrix `b' = e(b)
				local `n' = e(N)
				local `k' = colsof(`D')
				sort `touse' `cluster'
				by `touse' `cluster': gen byte `count' = 1 if _n==1  & `touse'
				summarize `count', meanonly
				local nclust = r(sum)
				local `sedof' = `nclust' - 1
				local clopt "cluster(`cluster')"
				local `dof' = ``n''-``k''-`dffe'+1-`ex_ct'
				local `kdffe' = ``k''+`dffe'-2+`ex_ct'
				local dfm = ``k''
				predict double `e' if `touse', residual


				tempvar  depvar 
				tempname b V XX YXX Xy X hat C BAA AA
				local newvarlist `yvar' `newxzlist' `feres'
				tokenize "`newvarlist'"
				local `depvar' = `yvar'
				matrix accum `YXX' = `newvarlist' if `touse', `constant'
				matrix `XX' = `YXX'[2...,2...]
				matrix `Xy' = `YXX'[1,2...]
				matrix `b' = `Xy' * syminv(`XX')
				matrix `hat' = `b' * `Xy''
				local sevarlist `yvar' `dmxvar' `zvar' `feres'
				tokenize "`sevarlist'"
				matrix accum `BAA' = `sevarlist' if `touse', `constant'
				matrix `AA' = `BAA'[2...,2...]
				matrix `C' = syminv(`AA') * (`YXX'[1,1] - `hat'[1,1]) / ``dof''
				matrix rownames `C' = `rows'
				matrix colnames `C' = `cols'
				matrix `D' = `C' 

				replace `touse'= e(sample)
				estimates post `b' `D', dof (``dof'') esample (`touse') depname("`yvar'") obs(``n'')
				_robust `e' if e(sample), minus(``kdffe'') `clopt'
				estimates matrix DM `C'
				estimates local vcetype "fevd clustered"
				estimates local depvar "`yvar'"
				estimates local cmd "xtfevd"
				est local invariant `zvar'
				est scalar df_fevd = ``dof''
				est scalar df_fe = `dffe'
				est scalar df_m = `dfm'
				}
				
				
				qui{	
					
					tempvar  error errs hats myhat yhat
		
					predict `yhat'
					gen `error' = `yvar'- `yhat'
					gen `errs' = `error'*`error'
					sum `errs', meanonly
					local mse1 = r(mean)
					local rss1 = r(sum)
					est scalar mse = r(mean)
					est scalar rss= r(sum)
					est scalar rmse = sqrt(r(mean))
					egen `myhat' = mean(`yhat')
					gen `hats' = (`yhat'-`myhat')*(`yhat'-`myhat')
					sum `hats', meanonly
					local ess1 = r(sum)
					est scalar ess= r(sum)
					
					local tss1=`rss1'+`ess1'
					est scalar tss=`rss1'+`ess1'
					est scalar r2 = `ess1' /`tss1'
					est scalar r2_a = 1-((`rss1'/``dof'')/(`tss1'/(``n''-1)))

					

					
					local testvarlist `newxzlist' `feres'
					test `testvarlist'
					
					est scalar F = r(F)
					est scalar F_p = r(p)
				
					}

			
				
				di
				di in gr "panel fixed effects regression with vector decomposition, cluster, 2nd step IV estimation"
				di
				di in gr "degrees of freedom fevd" _col(28) "=" in ye %9.0g e(df_fevd) _col(49) in gr "number of obs" _col(69) "=" in ye %9.0g e(N)
				di in gr "mean squared error" _col(28) "=" in ye %9.0g e(mse)  _col(49) in gr "F( " in ye e(df_m) in gr ", " in ye e(df_fevd) in gr ")" _col(69) "=" in ye %9.0g e(F)
				di in gr "root mean squared error" _col(28) "=" in ye %9.0g e(rmse) _col(49) in gr "Prob > F" _col(69) "=" in ye %9.0g e(F_p)	
				di in gr "Residual Sum of Squares" _col(28) "=" in ye %9.0g e(rss) _col(49) in gr "R-squared" _col(69) "=" in ye %9.0g e(r2)
				di in gr "Total Sum of Squares" _col(28) "=" in ye %9.0g e(tss) _col(49) in gr "adj. R-squared" _col(69) "=" in ye %9.0g e(r2_a)
				di in gr "Estimation Sum of Squares" _col(28) "=" in ye %9.0g e(ess)
				di
				estimates display

		if "`skew'" != "" {   
				di
				di in gr "skewness and normality test for the fixed effects of the first stage and the error term of the second stage
				di
				normtest `fe' `feres'
				}
	
		if "`corr'" != "" {
				di
				di in gr "correlation of fixed effects with explanatory variables"
				di
				corr `fe' `feres' `fullist'
				}
		
		else { di }
			
			
		}


		
		else {

			
			qui {
			
				xtreg `yvar' `newxzlist' if `touse', fe
				local dffe = e(df_a)
				local fe: permname ui
				predict `fe', u
				regress `fe' `zvar' if `touse', `constant'
				local feres: permname eta
				predict `feres', resid 
				tsset `ivar' `tvar'
				tempvar e k n dof kdffe
				tempvar count sedof
				tempname D b
				reg `yvar' `newxzlist' `feres' if `touse', `constant' mse1
				matrix `D' = e(V)
				local rows : rownames `D'
				local cols : colnames `D' 
				matrix `b' = e(b)
				local `n' = e(N)
				local `k' = colsof(`D')
				sort `touse' `cluster'
				by `touse' `cluster': gen byte `count' = 1 if _n==1  & `touse'
				summarize `count', meanonly
				local nclust = r(sum)
				local `sedof' = `nclust' - 1
				local clopt "cluster(`cluster')"
				local `dof' = ``n''-``k''-`dffe'+1
				local `kdffe' = ``k''+`dffe'-2
				local dfm = ``k''
				predict double `e' if `touse', residual

				tempvar  depvar 
				tempname b V XX YXX Xy X hat C BAA AA
				local newvarlist `yvar' `newxzlist' `feres'
				tokenize "`newvarlist'"
				local `depvar' = `yvar'
				matrix accum `YXX' = `newvarlist' if `touse', `constant'
				matrix `XX' = `YXX'[2...,2...]
				matrix `Xy' = `YXX'[1,2...]
				matrix `b' = `Xy' * syminv(`XX')
				matrix `hat' = `b' * `Xy''
				local sevarlist `yvar' `dmxvar' `zvar' `feres'
				tokenize "`sevarlist'"
				matrix accum `BAA' = `sevarlist' if `touse', `constant'
				matrix `AA' = `BAA'[2...,2...]
				matrix `C' = syminv(`AA') * (`YXX'[1,1] - `hat'[1,1]) / ``dof''
				matrix rownames `C' = `rows'
				matrix colnames `C' = `cols'
				matrix `D' = `C'
	
				replace `touse'= e(sample)
				estimates post `b' `D', dof (``dof'') esample (`touse') depname("`yvar'") obs(``n'')
				_robust `e' if e(sample), minus(``kdffe'') `clopt'
				estimates matrix DM `C'
		
				estimates local vcetype "fevd clustered"
				estimates local depvar "`yvar'"
				estimates local cmd "xtfevd"
				est local invariant `zvar'
				est scalar df_fevd = ``dof''
				est scalar df_fe = `dffe'
				est scalar df_m = `dfm'
				}
				
				
				qui{	
					
					tempvar  error errs hats myhat yhat
			
					predict `yhat'
					gen `error' = `yvar'- `yhat'
					gen `errs' = `error'*`error'
					sum `errs', meanonly
					local mse1 = r(mean)
					local rss1 = r(sum)
					est scalar mse = r(mean)
					est scalar rss= r(sum)
					est scalar rmse = sqrt(r(mean))
					egen `myhat' = mean(`yhat')
					gen `hats' = (`yhat'-`myhat')*(`yhat'-`myhat')
					sum `hats', meanonly
					local ess1 = r(sum)
					est scalar ess= r(sum)
					
					local tss1=`rss1'+`ess1'
					est scalar tss=`rss1'+`ess1'
					est scalar r2 = `ess1' /`tss1'
					est scalar r2_a = 1-((`rss1'/``dof'')/(`tss1'/(``n''-1)))

					

					
					local testvarlist `newxzlist' `feres'
					test `testvarlist'
					
					est scalar F = r(F)
					est scalar F_p = r(p)
				
					}

			
				
				di
				di in gr "panel fixed effects regression with vector decomposition, cluster"
				di
				di in gr "degrees of freedom fevd" _col(28) "=" in ye %9.0g e(df_fevd) _col(49) in gr "number of obs" _col(69) "=" in ye %9.0g e(N)
				di in gr "mean squared error" _col(28) "=" in ye %9.0g e(mse)  _col(49) in gr "F( " in ye e(df_m) in gr ", " in ye e(df_fevd) in gr ")" _col(69) "=" in ye %9.0g e(F)
				di in gr "root mean squared error" _col(28) "=" in ye %9.0g e(rmse) _col(49) in gr "Prob > F" _col(69) "=" in ye %9.0g e(F_p)	
				di in gr "Residual Sum of Squares" _col(28) "=" in ye %9.0g e(rss) _col(49) in gr "R-squared" _col(69) "=" in ye %9.0g e(r2)
				di in gr "Total Sum of Squares" _col(28) "=" in ye %9.0g e(tss) _col(49) in gr "adj. R-squared" _col(69) "=" in ye %9.0g e(r2_a)
				di in gr "Estimation Sum of Squares" _col(28) "=" in ye %9.0g e(ess)
				di
				estimates display

		if "`skew'" != "" {   
				di
				di in gr "skewness and normality test for the fixed effects of the first stage and the error term of the second stage
				di
				normtest `fe' `feres'
				}
	
		if "`corr'" != "" {
				di
				di in gr "correlation of fixed effects with explanatory variables"
				di
				corr `fe' `feres' `fullist'
				}
		
		else { di }
			
			}



		}	


	else if "`pcse'" != "" {


	if "`ar1'" != "" {



	if "`s2iv_endog'" != "" {
		
			
			qui{
				
				xtregar `yvar' `newxzlist' if `touse', fe 
				local dffe = e(df_a)
				local fe: permname ui
				predict `fe', u
				ivreg `fe' `iv1' (`ivendog'=`ivexog') if `touse', `constant'
				local feres: permname eta 
				predict `feres', resid 
				tsset `ivar' `tvar'
				tempname b V
				tempvar ypred
				qui prais `yvar' `newxzlist' `feres' if `touse', `corc' `rhotype' `constant' nolog /*nodw*/
				predict `ypred'
				matrix `V'= e(V)
				local rows : rownames `V'
				local cols : colnames `V'
				local n_obs = e(N)
				matrix `b' = e(b)
				local ll = e(ll)
				local ic = e(ic)
				local dw = e(dw)
				local dw_o = e(dw_0)
				local rho = e(rho)
				local newvarlist `yvar' `newxzlist' `feres'
				local lvar
				local praisvarlist
				foreach x of local newvarlist {
				tsset `ivar' `tvar'
				local i="_l"
 				local j= "_co"
 				tempvar `x'_tmp `x'_tmp2
  				gen ``x'_tmp'= l.`x'
  				gen ``x'_tmp2'= `x'-`rho'*``x'_tmp'
  				cap g `x'`i'=``x'_tmp'
  				cap g `x'`j'=``x'_tmp2'
  				local lvar `lvar' `x'`i'
  				local praisvarlist `praisvarlist' `x'`j'
					}	
				tsset `ivar' `tvar'		

				
				local typelistp praisvarlist
				local fullistp		/* full variable list */
	                        foreach type of local typelistp {
		                local `type' : list uniq `type'
		                tsunab `type' : ``type''
		                local fullistp : list fullistp | `type'
	                             }

				gettoken yvarp xzlistp : fullistp
				
				tempvar  k n dof kdffe depvar df_pcse 
				tempname D beta VPMAT XX YXX Xy X hat e esqr yhat C
				tempname VMAT
				tempvar r
				local newvarlist `yvarp' `xzlistp'
				tokenize "`newvarlist'"
				local `depvar' = `yvarp'
				qui xtpcse `dm_yvar' `dmxvar' `zvar' `feres' if `touse', `constant'
				matrix `VMAT' = e(V)
				local `r'= colsof(`VMAT')
				matrix accum `YXX' = `newvarlist' if `touse', `constant'
				local `n' = r(N)
				local `df_pcse' = ``n''-``r''
				local `dof' = ``n''- (rowsof(`YXX')-3+`ex_ct')
				local dfm = rowsof(`YXX')-1
				matrix `XX' = `YXX'[2...,2...]
				matrix `Xy' = `YXX'[1,2...]
				matrix `beta' = `Xy' * syminv(`XX')
				matrix `hat' = `beta' * `Xy''
				matrix `e' = ``depvar'' - `hat'
				matrix `C' =  (`VMAT'*``df_pcse'') / ``dof''
				matrix `VPMAT' = syminv(`XX') * (`YXX'[1,1] - `hat'[1,1]) / ``dof'' 
				matrix rownames `C' = `rows'
				matrix colnames `C' = `cols'  
				replace `touse'= e(sample)
				estimates post `b' `V', depname("`yvar'") obs(``n'') dof (``dof'') esample (`touse')
				estimates matrix DM `C'
				matrix `V' = e(DM)
				estimates repost V=`V'
				estimates matrix beta `beta'
				estimates local vcetype fevd_pcse
				estimates local depvar `yvar'
				estimates local cmd xtfevd
				est local invariant `zvar'
				est scalar df_fevd = ``dof''
				est scalar df_fe = `dffe'
				est scalar df_m = `dfm'
				est scalar rho = `rho'
				est scalar ll = `ll'
				est scalar ic = `ic'
				est scalar dw_t = `dw'
				est scalar dw_o = `dw_o'
				est scalar N = `n_obs'
				}

				
				qui{	
					
					tempvar  error errs hats myhat
					
					gen `error' = `yvar'- `ypred'
					gen `errs' = `error'*`error'
					sum `errs', meanonly
					local mse1 = r(mean)
					local rss1 = r(sum)
					est scalar mse = r(mean)
					est scalar rss= r(sum)
					est scalar rmse = sqrt(r(mean))
					egen `myhat' = mean(`ypred')
					gen `hats' = (`ypred'-`myhat')*(`ypred'-`myhat')
					sum `hats', meanonly
					local ess1 = r(sum)
					est scalar ess= r(sum)
					
					local tss1=`rss1'+`ess1'
					est scalar tss=`rss1'+`ess1'
					est scalar r2 = `ess1' /`tss1'
					est scalar r2_a = 1-((`rss1'/``dof'')/(`tss1'/(``n''-1)))

					

					
					local testvarlist `newxzlist' `feres' 
					test `testvarlist'
					
					est scalar F = r(F)
					est scalar F_p = r(p)
				
					}
				
				

			
				
				di
				di in gr "panel fixed effects regression with vector decomposition, panel corrected standard errors in stage 3, 2nd step IV estimation, and ar1 prais-winsten transformation"
				di
				di in gr "degrees of freedom fevd" _col(28) "=" in ye %9.0g e(df_fevd) _col(49) in gr "number of obs" _col(69) "=" in ye %9.0g e(N)
				di in gr "mean squared error" _col(28) "=" in ye %9.0g e(mse)  _col(49) in gr "F( " in ye e(df_m) in gr ", " in ye e(df_fevd) in gr ")" _col(69) "=" in ye %9.0g e(F)
				di in gr "root mean squared error" _col(28) "=" in ye %9.0g e(rmse) _col(49) in gr "Prob > F" _col(69) "=" in ye %9.0g e(F_p)	
				di in gr "Residual Sum of Squares" _col(28) "=" in ye %9.0g e(rss) _col(49) in gr "R-squared" _col(69) "=" in ye %9.0g e(r2)
				di in gr "Total Sum of Squares" _col(28) "=" in ye %9.0g e(tss) _col(49) in gr "adj. R-squared" _col(69) "=" in ye %9.0g e(r2_a)
				di in gr "Estimation Sum of Squares" _col(28) "=" in ye %9.0g e(ess)
				di
				estimates display
				di in gr "final rho after iterations" _col(40) "= " in ye %9.0g e(rho)
				di in gr "log likelihood" _col(40) "= " in ye %9.0g e(ll)
				di in gr "number of iterations" _col(40) "= " in ye %9.0g e(ic)
				di in gr "Durbin-Watson statistic (original)"  _col(40) "= " in ye %9.0g e(dw_o)
				di in gr "Durbin-Watson statistic (transformed)"  _col(40) "= " in ye %9.0g e(dw_t)


		if "`skew'" != "" {   
				di
				di in gr "skewness and normality test for the fixed effects of the first stage and the error term of the second stage
				di
				normtest `fe' `feres'
				}
	
		if "`corr'" != "" {
				di
				di in gr "correlation of fixed effects with explanatory variables"
				di
				corr `fe' `feres' `fullist'
				}
		else { di }

			
		}


		else {
		
			
			qui{
				
				xtregar `yvar' `newxzlist' if `touse', fe 
				local dffe = e(df_a)
				local fe: permname ui
				predict `fe', u
				regress `fe' `zvar' if `touse', `constant' 
				local feres: permname eta 
				predict `feres', resid 
				tsset `ivar' `tvar'
				tempname b V
				tempvar ypred
				qui prais `yvar' `newxzlist' `feres' if `touse', `corc' `rhotype' `constant' nolog /*nodw*/
				predict `ypred'
				matrix `V'= e(V)
				local rows : rownames `V'
				local cols : colnames `V'
				local n_obs = e(N)
				matrix `b' = e(b)
				local ll = e(ll)
				local ic = e(ic)
				local dw = e(dw)
				local dw_o = e(dw_0)
				local rho = e(rho)
				local newvarlist `yvar' `newxzlist' `feres'
				local lvar
				local praisvarlist
				foreach x of local newvarlist {
				tsset `ivar' `tvar'
				local i="_l"
 				local j= "_co"
 				tempvar `x'_tmp `x'_tmp2
  				gen ``x'_tmp'= l.`x'
  				gen ``x'_tmp2'= `x'-`rho'*``x'_tmp'
  				cap g `x'`i'=``x'_tmp'
  				cap g `x'`j'=``x'_tmp2'
  				local lvar `lvar' `x'`i'
  				local praisvarlist `praisvarlist' `x'`j'
					}	
				tsset `ivar' `tvar'		

				
				local typelistp praisvarlist
				local fullistp		/* full variable list */
	                        foreach type of local typelistp {
		                local `type' : list uniq `type'
		                tsunab `type' : ``type''
		                local fullistp : list fullistp | `type'
	                             }

				gettoken yvarp xzlistp : fullistp
				
				tempvar  k n dof kdffe depvar df_pcse 
				tempname D beta VPMAT XX YXX Xy X hat e esqr yhat C
				tempname VMAT
				tempvar r
				local newvarlist `yvarp' `xzlistp'
				tokenize "`newvarlist'"
				local `depvar' = `yvarp'
				qui xtpcse `dm_yvar' `dmxvar' `zvar' `feres' if `touse', `constant'
				matrix `VMAT' = e(V)
				local `r'= colsof(`VMAT')
				matrix accum `YXX' = `newvarlist' if `touse', `constant'
				local `n' = r(N)
				local `df_pcse' = ``n''-``r''
				local `dof' = ``n''- (rowsof(`YXX')-3)
				local dfm = rowsof(`YXX')-1
				matrix `XX' = `YXX'[2...,2...]
				matrix `Xy' = `YXX'[1,2...]
				matrix `beta' = `Xy' * syminv(`XX')
				matrix `hat' = `beta' * `Xy''
				matrix `e' = ``depvar'' - `hat'
				matrix `C' =  (`VMAT'*``df_pcse'') / ``dof''
				matrix `VPMAT' = syminv(`XX') * (`YXX'[1,1] - `hat'[1,1]) / ``dof'' 
				matrix rownames `C' = `rows'
				matrix colnames `C' = `cols'  
				replace `touse'= e(sample)
				estimates post `b' `V', depname("`yvar'") obs(``n'') dof (``dof'') esample (`touse')
				estimates matrix DM `C'
				matrix `V' = e(DM)
				estimates repost V=`V'
				estimates matrix beta `beta'
				estimates local vcetype fevd_pcse
				estimates local depvar `yvar'
				estimates local cmd xtfevd
				est local invariant `zvar'
				est scalar df_fevd = ``dof''
				est scalar df_fe = `dffe'
				est scalar df_m = `dfm'
				est scalar rho = `rho'
				est scalar ll = `ll'
				est scalar ic = `ic'
				est scalar dw_t = `dw'
				est scalar dw_o = `dw_o'
				est scalar N = `n_obs'
				}

				
				qui{	
					
					tempvar  error errs hats myhat
					
					gen `error' = `yvar'- `ypred'
					gen `errs' = `error'*`error'
					sum `errs', meanonly
					local mse1 = r(mean)
					local rss1 = r(sum)
					est scalar mse = r(mean)
					est scalar rss= r(sum)
					est scalar rmse = sqrt(r(mean))
					egen `myhat' = mean(`ypred')
					gen `hats' = (`ypred'-`myhat')*(`ypred'-`myhat')
					sum `hats', meanonly
					local ess1 = r(sum)
					est scalar ess= r(sum)
					
					local tss1=`rss1'+`ess1'
					est scalar tss=`rss1'+`ess1'
					est scalar r2 = `ess1' /`tss1'
					est scalar r2_a = 1-((`rss1'/``dof'')/(`tss1'/(``n''-1)))

					

					
					local testvarlist `newxzlist' `feres' 
					test `testvarlist'
					
					est scalar F = r(F)
					est scalar F_p = r(p)
				
					}
				
				

			
				
				di
				di in gr "panel fixed effects regression with vector decomposition, panel corrected standard errors in stage 3, and ar1 prais-winsten transformation"
				di
				di in gr "degrees of freedom fevd" _col(28) "=" in ye %9.0g e(df_fevd) _col(49) in gr "number of obs" _col(69) "=" in ye %9.0g e(N)
				di in gr "mean squared error" _col(28) "=" in ye %9.0g e(mse)  _col(49) in gr "F( " in ye e(df_m) in gr ", " in ye e(df_fevd) in gr ")" _col(69) "=" in ye %9.0g e(F)
				di in gr "root mean squared error" _col(28) "=" in ye %9.0g e(rmse) _col(49) in gr "Prob > F" _col(69) "=" in ye %9.0g e(F_p)	
				di in gr "Residual Sum of Squares" _col(28) "=" in ye %9.0g e(rss) _col(49) in gr "R-squared" _col(69) "=" in ye %9.0g e(r2)
				di in gr "Total Sum of Squares" _col(28) "=" in ye %9.0g e(tss) _col(49) in gr "adj. R-squared" _col(69) "=" in ye %9.0g e(r2_a)
				di in gr "Estimation Sum of Squares" _col(28) "=" in ye %9.0g e(ess)
				di
				estimates display
				di in gr "final rho after iterations" _col(40) "= " in ye %9.0g e(rho)
				di in gr "log likelihood" _col(40) "= " in ye %9.0g e(ll)
				di in gr "number of iterations" _col(40) "= " in ye %9.0g e(ic)
				di in gr "Durbin-Watson statistic (original)"  _col(40) "= " in ye %9.0g e(dw_o)
				di in gr "Durbin-Watson statistic (transformed)"  _col(40) "= " in ye %9.0g e(dw_t)


		if "`skew'" != "" {   
				di
				di in gr "skewness and normality test for the fixed effects of the first stage and the error term of the second stage
				di
				normtest `fe' `feres'
				}
	
		if "`corr'" != "" {
				di
				di in gr "correlation of fixed effects with explanatory variables"
				di
				corr `fe' `feres' `fullist'
				}
		else { di }

			
			}
		}




    		else if "`s2iv_endog'" != "" {
		
			
			qui{
				
				xtreg `yvar' `newxzlist' if `touse', fe 
				local dffe = e(df_a)
				local fe: permname ui
				predict `fe', u
				ivreg `fe' `iv1' (`ivendog'=`ivexog') if `touse', `constant' 
				local feres: permname eta 
				predict `feres', resid 
				tsset `ivar' `tvar'
				tempname VMAT
				tempvar r
				xtpcse `dm_yvar' `dmxvar' `zvar' `feres' if `touse', `constant'
				matrix `VMAT' = e(V)
				local `r'= colsof(`VMAT')
				
				tempvar  k n dof kdffe depvar df_pcse 
				tempname D b V XX YXX Xy X hat e esqr yhat C
				local newvarlist `yvar' `newxzlist' `feres'
				tokenize "`newvarlist'"
				local `depvar' = `yvar'
				matrix accum `YXX' = `newvarlist' if `touse', `constant'
				local `n' = r(N)
				local `df_pcse' = ``n''-``r''
				local `dof' = ``n''- (rowsof(`YXX')+`dffe'-2+`ex_ct')
				local dfm = rowsof(`YXX')-1
				matrix `XX' = `YXX'[2...,2...]
				matrix `Xy' = `YXX'[1,2...]
				matrix `b' = `Xy' * syminv(`XX')
				matrix `hat' = `b' * `Xy''
				matrix `e' = ``depvar'' - `hat'
				matrix `C' =  (`VMAT'*``df_pcse'') / ``dof''
				matrix `V' = syminv(`XX') * (`YXX'[1,1] - `hat'[1,1]) / ``dof'' 
				local rows : rownames `V'
				local cols : colnames `V' 
				matrix rownames `C' = `rows'
				matrix colnames `C' = `cols'  
				replace `touse'= e(sample)
				estimates post `b' `V', depname("`yvar'") obs(``n'') dof (``dof'') esample (`touse')
				estimates matrix DM `C'
				matrix `V' = e(DM)
				estimates repost V=`V'
				estimates local vcetype fevd_pcse
				estimates local depvar `yvar'
				estimates local cmd xtfevd
				est local invariant `zvar'
				est scalar df_fevd = ``dof''
				est scalar df_fe = `dffe'
				est scalar df_m = `dfm'
				}
				
				
				qui{	
					
					tempvar  error errs hats myhat yhat
					predict `yhat'
					gen `error' = `yvar'- `yhat'
					gen `errs' = `error'*`error'
					sum `errs', meanonly
					local mse1 = r(mean)
					local rss1 = r(sum)
					est scalar mse = r(mean)
					est scalar rss= r(sum)
					est scalar rmse = sqrt(r(mean))
					egen `myhat' = mean(`yhat')
					gen `hats' = (`yhat'-`myhat')*(`yhat'-`myhat')
					sum `hats', meanonly
					local ess1 = r(sum)
					est scalar ess= r(sum)
					
					local tss1=`rss1'+`ess1'
					est scalar tss=`rss1'+`ess1'
					est scalar r2 = `ess1' /`tss1'
					est scalar r2_a = 1-((`rss1'/``dof'')/(`tss1'/(``n''-1)))

					

					
					local testvarlist `newxzlist' `feres'
					test `testvarlist'
					
					est scalar F = r(F)
					est scalar F_p = r(p)
				
					}

			
				
				di
				di in gr "panel fixed effects regression with vector decomposition, panel corrected standard errors in stage 3, 2nd step IV estimation"
				di
				di in gr "degrees of freedom fevd" _col(28) "=" in ye %9.0g e(df_fevd) _col(49) in gr "number of obs" _col(69) "=" in ye %9.0g e(N)
				di in gr "mean squared error" _col(28) "=" in ye %9.0g e(mse)  _col(49) in gr "F( " in ye e(df_m) in gr ", " in ye e(df_fevd) in gr ")" _col(69) "=" in ye %9.0g e(F)
				di in gr "root mean squared error" _col(28) "=" in ye %9.0g e(rmse) _col(49) in gr "Prob > F" _col(69) "=" in ye %9.0g e(F_p)	
				di in gr "Residual Sum of Squares" _col(28) "=" in ye %9.0g e(rss) _col(49) in gr "R-squared" _col(69) "=" in ye %9.0g e(r2)
				di in gr "Total Sum of Squares" _col(28) "=" in ye %9.0g e(tss) _col(49) in gr "adj. R-squared" _col(69) "=" in ye %9.0g e(r2_a)
				di in gr "Estimation Sum of Squares" _col(28) "=" in ye %9.0g e(ess)
				di
				estimates display

		if "`skew'" != "" {   
				di
				di in gr "skewness and normality test for the fixed effects of the first stage and the error term of the second stage
				di
				normtest `fe' `feres'
				}
	
		if "`corr'" != "" {
				di
				di in gr "correlation of fixed effects with explanatory variables"
				di
				corr `fe' `feres' `fullist'
				}
		else { di }
		
	
			}


    		else {
		
			
			qui{
				
				xtreg `yvar' `newxzlist' if `touse', fe 
				local dffe = e(df_a)
				local fe: permname ui
				predict `fe', u
				regress `fe' `zvar' if `touse', `constant' 
				local feres: permname eta 
				predict `feres', resid 
				tsset `ivar' `tvar'
				tempname VMAT
				tempvar r
				xtpcse `dm_yvar' `dmxvar' `zvar' `feres' if `touse', `constant'
				matrix `VMAT' = e(V)
				local `r'= colsof(`VMAT')
				
				tempvar  k n dof kdffe depvar df_pcse 
				tempname D b V XX YXX Xy X hat e esqr C
				local newvarlist `yvar' `newxzlist' `feres'
				tokenize "`newvarlist'"
				local `depvar' = `yvar'
				matrix accum `YXX' = `newvarlist' if `touse', `constant'
				local `n' = r(N)
				local `df_pcse' = ``n''-``r''
				local `dof' = ``n''- (rowsof(`YXX')+`dffe'-2)
				local dfm = rowsof(`YXX')-1
				matrix `XX' = `YXX'[2...,2...]
				matrix `Xy' = `YXX'[1,2...]
				matrix `b' = `Xy' * syminv(`XX')
				matrix `hat' = `b' * `Xy''
				
				matrix `e' = ``depvar'' - `hat'
				matrix `C' =  (`VMAT'*``df_pcse'') / ``dof''
				matrix `V' = syminv(`XX') * (`YXX'[1,1] - `hat'[1,1]) / ``dof'' 
				local rows : rownames `V'
				local cols : colnames `V' 
				matrix rownames `C' = `rows'
				matrix colnames `C' = `cols' 
				
				replace `touse'= e(sample)
				estimates post `b' `V', depname("`yvar'") obs(``n'') dof (``dof'') esample (`touse')
				estimates matrix DM `C'
				matrix `V' = e(DM)
				estimates repost V=`V'
 				
				estimates local vcetype fevd_pcse
				estimates local depvar `yvar'
				estimates local cmd xtfevd
				est local invariant `zvar'
				est scalar df_fevd = ``dof''
				est scalar df_fe = `dffe'
				est scalar df_m = `dfm'
				}
				
				
				qui{	
					
					tempvar  error errs hats myhat yhat
					predict `yhat'
					gen `error' = `yvar'- `yhat'
					gen `errs' = `error'*`error'
					sum `errs', meanonly
					local mse1 = r(mean)
					local rss1 = r(sum)
					est scalar mse = r(mean)
					est scalar rss= r(sum)
					est scalar rmse = sqrt(r(mean))
					egen `myhat' = mean(`yhat')
					gen `hats' = (`yhat'-`myhat')*(`yhat'-`myhat')
					sum `hats', meanonly
					local ess1 = r(sum)
					est scalar ess= r(sum)
					
					local tss1=`rss1'+`ess1'
					est scalar tss=`rss1'+`ess1'
					est scalar r2 = `ess1' /`tss1'
					est scalar r2_a = 1-((`rss1'/``dof'')/(`tss1'/(``n''-1)))

					

					
					local testvarlist `newxzlist' `feres'
					test `testvarlist'
					
					est scalar F = r(F)
					est scalar F_p = r(p)
				
					}

			
				
				di
				di in gr "panel fixed effects regression with vector decomposition, panel corrected standard errors in stage 3"
				di
				di in gr "degrees of freedom fevd" _col(28) "=" in ye %9.0g e(df_fevd) _col(49) in gr "number of obs" _col(69) "=" in ye %9.0g e(N)
				di in gr "mean squared error" _col(28) "=" in ye %9.0g e(mse)  _col(49) in gr "F( " in ye e(df_m) in gr ", " in ye e(df_fevd) in gr ")" _col(69) "=" in ye %9.0g e(F)
				di in gr "root mean squared error" _col(28) "=" in ye %9.0g e(rmse) _col(49) in gr "Prob > F" _col(69) "=" in ye %9.0g e(F_p)	
				di in gr "Residual Sum of Squares" _col(28) "=" in ye %9.0g e(rss) _col(49) in gr "R-squared" _col(69) "=" in ye %9.0g e(r2)
				di in gr "Total Sum of Squares" _col(28) "=" in ye %9.0g e(tss) _col(49) in gr "adj. R-squared" _col(69) "=" in ye %9.0g e(r2_a)
				di in gr "Estimation Sum of Squares" _col(28) "=" in ye %9.0g e(ess)
				di
				estimates display

		if "`skew'" != "" {   
				di
				di in gr "skewness and normality test for the fixed effects of the first stage and the error term of the second stage
				di
				normtest `fe' `feres'
				}
	
		if "`corr'" != "" {
				di
				di in gr "correlation of fixed effects with explanatory variables"
				di
				corr `fe' `feres' `fullist'
				}
		else { di }
		
	
			}


		}




	else if "`s2iv_endog'" != "" {

	if "`ar1'" != "" {
		
			
			qui{
				
				xtregar `yvar' `newxzlist' if `touse', fe 
				local dffe = e(df_a)
				local fe: permname ui
				predict `fe', u
				ivreg `fe' `iv1' (`ivendog'=`ivexog') if `touse', `constant'
				local feres: permname eta 
				predict `feres', resid 
				tsset `ivar' `tvar'
				tempname b V
				tempvar ypred
				qui prais `yvar' `newxzlist' `feres' if `touse', `corc' `rhotype' `constant' nolog /*nodw*/
				predict `ypred'
				matrix `V'= e(V)
				local rows : rownames `V'
				local cols : colnames `V'
				local n_obs = e(N)
				matrix `b' = e(b)
				local ll = e(ll)
				local ic = e(ic)
				local dw = e(dw)
				local dw_o = e(dw_0)
				local rho = e(rho)
				local newvarlist `yvar' `newxzlist' `feres'
				local lvar
				local praisvarlist
				foreach x of local newvarlist {
				tsset `ivar' `tvar'
				local i="_l"
 				local j= "_co"
 				tempvar `x'_tmp `x'_tmp2
  				gen ``x'_tmp'= l.`x'
  				gen ``x'_tmp2'= `x'-`rho'*``x'_tmp'
  				cap g `x'`i'=``x'_tmp'
  				cap g `x'`j'=``x'_tmp2'
  				local lvar `lvar' `x'`i'
  				local praisvarlist `praisvarlist' `x'`j'
					}	
				tsset `ivar' `tvar'			


				

				
				local typelistp praisvarlist
				local fullistp		/* full variable list */
	                        foreach type of local typelistp {
		                local `type' : list uniq `type'
		                tsunab `type' : ``type''
		                local fullistp : list fullistp | `type'
	                             }

				gettoken yvarp xzlistp : fullistp
				
				
				tempvar  k n dof kdffe depvar 
				tempname D beta VMAT XX YXX Xy X hat e esqr C BAA AA
				local newvarlist `yvarp' `xzlistp'
				tokenize "`newvarlist'"
				local `depvar' = `yvarp'
				matrix accum `YXX' = `newvarlist' if `touse', `constant'
				local `n' = r(N)
				local `dof' = ``n''- (rowsof(`YXX')-3+`ex_ct')
				local dfm = rowsof(`YXX')-1
				matrix `XX' = `YXX'[2...,2...]
				matrix `Xy' = `YXX'[1,2...]
				matrix `beta' = `Xy' * syminv(`XX')
				matrix `hat' = `beta' * `Xy''
				matrix `e' = ``depvar'' - `hat'
				matrix `VMAT' = syminv(`XX') * (`YXX'[1,1] - `hat'[1,1]) / ``dof'' 

				local sevarlist `yvar' `dmxvar' `zvar' `feres'
				tokenize "`sevarlist'"
				matrix accum `BAA' = `sevarlist' if `touse', `constant'
				matrix `AA' = `BAA'[2...,2...]
				matrix `C' = syminv(`AA') * (`YXX'[1,1] - `hat'[1,1]) / ``dof''

				matrix rownames `C' = `rows'
				matrix colnames `C' = `cols'
	
				replace `touse'= e(sample)
				estimates post `b' `V', depname("`yvar'") obs(``n'') dof (``dof'') esample (`touse')
				estimates matrix DM `C'
				matrix `V' = e(DM)
				estimates repost V=`V'
				estimates matrix beta `beta'
				estimates local vcetype fevd
				estimates local depvar `yvar'
				estimates local cmd xtfevd
				est local invariant `zvar'
				est scalar df_fevd = ``dof''
				est scalar df_fe = `dffe'
				est scalar df_m = `dfm'
				est scalar rho = `rho'
				est scalar ll = `ll'
				est scalar ic = `ic'
				est scalar dw_t = `dw'
				est scalar dw_o = `dw_o'
				est scalar N = `n_obs'
				
				}
				
				qui{	
					
					tempvar  error errs hats myhat
					
					gen `error' = `yvar'- `ypred'
					gen `errs' = `error'*`error'
					sum `errs', meanonly
					local mse1 = r(mean)
					local rss1 = r(sum)
					est scalar mse = r(mean)
					est scalar rss= r(sum)
					est scalar rmse = sqrt(r(mean))
					egen `myhat' = mean(`ypred')
					gen `hats' = (`ypred'-`myhat')*(`ypred'-`myhat')
					sum `hats', meanonly
					local ess1 = r(sum)
					est scalar ess= r(sum)
					
					local tss1=`rss1'+`ess1'
					est scalar tss=`rss1'+`ess1'
					est scalar r2 = `ess1' /`tss1'
					est scalar r2_a = 1-((`rss1'/``dof'')/(`tss1'/(``n''-1)))

					

					
					local testvarlist `newxzlist' `feres' 
					test `testvarlist'
					
					est scalar F = r(F)
					est scalar F_p = r(p)
				
					}
				
				

			
				
				di
				di in gr "panel fixed effects regression with vector decomposition, 2nd step IV estimation, and ar1 prais-winsten transformation"
				di
				di in gr "degrees of freedom fevd" _col(28) "=" in ye %9.0g e(df_fevd) _col(49) in gr "number of obs" _col(69) "=" in ye %9.0g e(N)
				di in gr "mean squared error" _col(28) "=" in ye %9.0g e(mse)  _col(49) in gr "F( " in ye e(df_m) in gr ", " in ye e(df_fevd) in gr ")" _col(69) "=" in ye %9.0g e(F)
				di in gr "root mean squared error" _col(28) "=" in ye %9.0g e(rmse) _col(49) in gr "Prob > F" _col(69) "=" in ye %9.0g e(F_p)	
				di in gr "Residual Sum of Squares" _col(28) "=" in ye %9.0g e(rss) _col(49) in gr "R-squared" _col(69) "=" in ye %9.0g e(r2)
				di in gr "Total Sum of Squares" _col(28) "=" in ye %9.0g e(tss) _col(49) in gr "adj. R-squared" _col(69) "=" in ye %9.0g e(r2_a)
				di in gr "Estimation Sum of Squares" _col(28) "=" in ye %9.0g e(ess)
				di
				estimates display
				di in gr "final rho after iterations" _col(40) "= " in ye %9.0g e(rho)
				di in gr "log likelihood" _col(40) "= " in ye %9.0g e(ll)
				di in gr "number of iterations" _col(40) "= " in ye %9.0g e(ic)
				di in gr "Durbin-Watson statistic (original)"  _col(40) "= " in ye %9.0g e(dw_o)
				di in gr "Durbin-Watson statistic (transformed)"  _col(40) "= " in ye %9.0g e(dw_t)


		if "`skew'" != "" {   
				di
				di in gr "skewness and normality test for the fixed effects of the first stage and the error term of the second stage
				di
				normtest `fe' `feres'
				}
	
		if "`corr'" != "" {
				di
				di in gr "correlation of fixed effects with explanatory variables"
				di
				corr `fe' `feres' `fullist'
				}
		else { di }	

			
		}


		else {
		
			
			qui{
				
				xtreg `yvar' `newxzlist' if `touse', fe 
				local dffe = e(df_a)
				local fe: permname ui
				predict `fe', u
				ivreg `fe' `iv1' (`ivendog'=`ivexog') if `touse', `constant' 
				local feres: permname eta 
				predict `feres', resid 
				tsset `ivar' `tvar'
				
				tempvar  k n dof kdffe depvar 
				tempname D b V XX YXX Xy X hat e esqr C BAA AA
				local newvarlist `yvar' `newxzlist' `feres'
				tokenize "`newvarlist'"
				local `depvar' = `yvar'
				matrix accum `YXX' = `newvarlist' if `touse', `constant'
				local `n' = r(N)
				local `dof' = ``n''- (rowsof(`YXX')+`dffe'-2+`ex_ct')
				local dfm = rowsof(`YXX')-1
				matrix `XX' = `YXX'[2...,2...]
				matrix `Xy' = `YXX'[1,2...]
				matrix `b' = `Xy' * syminv(`XX')
				matrix `hat' = `b' * `Xy''
				
				matrix `e' = ``depvar'' - `hat'
				matrix `V' = syminv(`XX') * (`YXX'[1,1] - `hat'[1,1]) / ``dof''
				local rows : rownames `V'
				local cols : colnames `V'
				local sevarlist `yvar' `dmxvar' `zvar' `feres'
				tokenize "`sevarlist'"
				matrix accum `BAA' = `sevarlist' if `touse', `constant'
				matrix `AA' = `BAA'[2...,2...]
				matrix `C' = syminv(`AA') * (`YXX'[1,1] - `hat'[1,1]) / ``dof''
				matrix rownames `C' = `rows'
				matrix colnames `C' = `cols' 
				
				replace `touse'= e(sample)
				estimates post `b' `V', depname("`yvar'") obs(``n'') dof (``dof'') esample (`touse')
				estimates matrix DM `C'
				matrix `V' = e(DM)
				estimates repost V=`V'
 				
				estimates local vcetype fevd
				estimates local depvar `yvar'
				estimates local cmd xtfevd
				est local invariant `zvar'
				est scalar df_fevd = ``dof''
				est scalar df_fe = `dffe'
				est scalar df_m = `dfm'
				}
				
				
				qui{	
					
					tempvar  error errs hats myhat yhat
					
					predict `yhat'
					gen `error' = `yvar'- `yhat'
					gen `errs' = `error'*`error'
					sum `errs', meanonly
					local mse1 = r(mean)
					local rss1 = r(sum)
					est scalar mse = r(mean)
					est scalar rss= r(sum)
					est scalar rmse = sqrt(r(mean))
					egen `myhat' = mean(`yhat')
					gen `hats' = (`yhat'-`myhat')*(`yhat'-`myhat')
					sum `hats', meanonly
					local ess1 = r(sum)
					est scalar ess= r(sum)
					
					local tss1=`rss1'+`ess1'
					est scalar tss=`rss1'+`ess1'
					est scalar r2 = `ess1' /`tss1'
					est scalar r2_a = 1-((`rss1'/``dof'')/(`tss1'/(``n''-1)))

					

					
					local testvarlist `newxzlist' `feres'
					test `testvarlist'
					
					est scalar F = r(F)
					est scalar F_p = r(p)
				
					}

			
				
				di
				di in gr "panel fixed effects regression with vector decomposition, 2nd step IV estimation"
				di
				di in gr "degrees of freedom fevd" _col(28) "=" in ye %9.0g e(df_fevd) _col(49) in gr "number of obs" _col(69) "=" in ye %9.0g e(N)
				di in gr "mean squared error" _col(28) "=" in ye %9.0g e(mse)  _col(49) in gr "F( " in ye e(df_m) in gr ", " in ye e(df_fevd) in gr ")" _col(69) "=" in ye %9.0g e(F)
				di in gr "root mean squared error" _col(28) "=" in ye %9.0g e(rmse) _col(49) in gr "Prob > F" _col(69) "=" in ye %9.0g e(F_p)	
				di in gr "Residual Sum of Squares" _col(28) "=" in ye %9.0g e(rss) _col(49) in gr "R-squared" _col(69) "=" in ye %9.0g e(r2)
				di in gr "Total Sum of Squares" _col(28) "=" in ye %9.0g e(tss) _col(49) in gr "adj. R-squared" _col(69) "=" in ye %9.0g e(r2_a)
				di in gr "Estimation Sum of Squares" _col(28) "=" in ye %9.0g e(ess)
				di
				estimates display

		if "`skew'" != "" {   
				di
				di in gr "skewness and normality test for the fixed effects of the first stage and the error term of the second stage
				di
				normtest `fe' `feres'
				}
	
		if "`corr'" != "" {
				di
				di in gr "correlation of fixed effects with explanatory variables"
				di
				corr `fe' `feres' `fullist'
				}
		else { di }

			}	
	
		}


	else if "`ar1'" != "" {
		
			
			qui{
				
				xtregar `yvar' `newxzlist' if `touse', fe 
				local dffe = e(df_a)
				local fe: permname ui
				predict `fe', u
				regress `fe' `zvar' if `touse', `constant' 
				local feres: permname eta 
				predict `feres', resid 
				tsset `ivar' `tvar'
				tempname b V
				tempvar ypred 
				qui prais `yvar' `newxzlist' `feres' if `touse', `corc' `rhotype' `constant' nolog /*nodw*/
				predict `ypred'
				matrix `V'= e(V)
				local rows : rownames `V'
				local cols : colnames `V'
				local n_obs = e(N)
				matrix `b' = e(b)
				local ll = e(ll)
				local ic = e(ic)
				local dw = e(dw)
				local dw_o = e(dw_0)
				local rho = e(rho)
				local newvarlist `yvar' `newxzlist' `feres'
				local lvar
				local praisvarlist
				foreach x of local newvarlist {
				tsset `ivar' `tvar'
				local i="_l"
 				local j= "_co"
 				tempvar `x'_tmp `x'_tmp2
  				gen ``x'_tmp'= l.`x'
  				gen ``x'_tmp2'= `x'-`rho'*``x'_tmp'
  				cap g `x'`i'=``x'_tmp'
  				cap g `x'`j'=``x'_tmp2'
  				local lvar `lvar' `x'`i'
  				local praisvarlist `praisvarlist' `x'`j'
					}	
				tsset `ivar' `tvar'			

				

				
				local typelistp praisvarlist
				local fullistp		/* full variable list */
	                        foreach type of local typelistp {
		                local `type' : list uniq `type'
		                tsunab `type' : ``type''
		                local fullistp : list fullistp | `type'
	                             }

				gettoken yvarp xzlistp : fullistp
				
				
				tempvar  k n dof kdffe depvar 
				tempname D beta VMAT XX YXX Xy X hat e esqr C BAA AA
				local newvarlist `yvarp' `xzlistp'
				tokenize "`newvarlist'"
				local `depvar' = `yvarp'
				matrix accum `YXX' = `newvarlist' if `touse', `constant'
				local `n' = r(N)
				local `dof' = ``n''- (rowsof(`YXX')-3)
				local dfm = rowsof(`YXX')-1
				matrix `XX' = `YXX'[2...,2...]
				matrix `Xy' = `YXX'[1,2...]
				matrix `beta' = `Xy' * syminv(`XX')
				matrix `hat' = `beta' * `Xy''
				matrix `e' = ``depvar'' - `hat'
				matrix `VMAT' = syminv(`XX') * (`YXX'[1,1] - `hat'[1,1]) / ``dof'' 

				local sevarlist `yvar' `dmxvar' `zvar' `feres'
				tokenize "`sevarlist'"
				matrix accum `BAA' = `sevarlist' if `touse', `constant'
				matrix `AA' = `BAA'[2...,2...]
				matrix `C' = syminv(`AA') * (`YXX'[1,1] - `hat'[1,1]) / ``dof''

				matrix rownames `C' = `rows'
				matrix colnames `C' = `cols'
	
				replace `touse'= e(sample)
				estimates post `b' `V', depname("`yvar'") obs(``n'') dof (``dof'') esample (`touse')
				estimates matrix DM `C'
				matrix `V' = e(DM)
				estimates repost V=`V'
				estimates matrix beta `beta' 				
				estimates local vcetype fevd
				estimates local depvar `yvar'
				estimates local cmd xtfevd
				est local invariant `zvar'
				est scalar df_fevd = ``dof''
				est scalar df_fe = `dffe'
				est scalar df_m = `dfm'
				est scalar rho = `rho'
				est scalar ll = `ll'
				est scalar ic = `ic'
				est scalar dw_t = `dw'
				est scalar dw_o = `dw_o'
				est scalar N = `n_obs'
				}

				
				qui{	
					
					tempvar  error errs hats myhat
					
					gen `error' = `yvar'- `ypred'
					gen `errs' = `error'*`error'
					sum `errs', meanonly
					local mse1 = r(mean)
					local rss1 = r(sum)
					est scalar mse = r(mean)
					est scalar rss= r(sum)
					est scalar rmse = sqrt(r(mean))
					egen `myhat' = mean(`ypred')
					gen `hats' = (`ypred'-`myhat')*(`ypred'-`myhat')
					sum `hats', meanonly
					local ess1 = r(sum)
					est scalar ess= r(sum)
					
					local tss1=`rss1'+`ess1'
					est scalar tss=`rss1'+`ess1'
					est scalar r2 = `ess1' /`tss1'
					est scalar r2_a = 1-((`rss1'/``dof'')/(`tss1'/(``n''-1)))

					

					
					local testvarlist `newxzlist' `feres' 
					test `testvarlist'
					
					est scalar F = r(F)
					est scalar F_p = r(p)
				
					}
				
				

			
				
				di
				di in gr "panel fixed effects regression with vector decomposition and ar1 prais-winsten transformation"
				di
				di in gr "degrees of freedom fevd" _col(28) "=" in ye %9.0g e(df_fevd) _col(49) in gr "number of obs" _col(69) "=" in ye %9.0g e(N)
				di in gr "mean squared error" _col(28) "=" in ye %9.0g e(mse)  _col(49) in gr "F( " in ye e(df_m) in gr ", " in ye e(df_fevd) in gr ")" _col(69) "=" in ye %9.0g e(F)
				di in gr "root mean squared error" _col(28) "=" in ye %9.0g e(rmse) _col(49) in gr "Prob > F" _col(69) "=" in ye %9.0g e(F_p)	
				di in gr "Residual Sum of Squares" _col(28) "=" in ye %9.0g e(rss) _col(49) in gr "R-squared" _col(69) "=" in ye %9.0g e(r2)
				di in gr "Total Sum of Squares" _col(28) "=" in ye %9.0g e(tss) _col(49) in gr "adj. R-squared" _col(69) "=" in ye %9.0g e(r2_a)
				di in gr "Estimation Sum of Squares" _col(28) "=" in ye %9.0g e(ess)
				di
				estimates display
				di in gr "final rho after iterations" _col(40) "= " in ye %9.0g e(rho)
				di in gr "log likelihood" _col(40) "= " in ye %9.0g e(ll)
				di in gr "number of iterations" _col(40) "= " in ye %9.0g e(ic)
				di in gr "Durbin-Watson statistic (original)"  _col(40) "= " in ye %9.0g e(dw_o)
				di in gr "Durbin-Watson statistic (transformed)"  _col(40) "= " in ye %9.0g e(dw_t)


		if "`skew'" != "" {   
				di
				di in gr "skewness and normality test for the fixed effects of the first stage and the error term of the second stage
				di
				normtest `fe' `feres'
				}
	
		if "`corr'" != "" {
				di
				di in gr "correlation of fixed effects with explanatory variables"
				di
				corr `fe' `feres' `fullist'
				}
		else { di }

			

			
		}





	else {		
			
			
			qui{
				
				xtreg `yvar' `newxzlist' if `touse', fe 
				local dffe = e(df_a)
				local fe: permname ui
				predict `fe', u
				regress `fe' `zvar' if `touse', `constant' 
				local feres: permname eta 
				predict `feres', resid 
				tsset `ivar' `tvar'
				
				tempvar  k n dof kdffe depvar 
				tempname D b V XX YXX Xy X hat e esqr C BAA AA
				local newvarlist `yvar' `newxzlist' `feres'
				tokenize "`newvarlist'"
				local `depvar' = `yvar'
				matrix accum `YXX' = `newvarlist' if `touse', `constant'
				local `n' = r(N)
				local `dof' = ``n''- (rowsof(`YXX')+`dffe'-2)
				local dfm = rowsof(`YXX')-1
				matrix `XX' = `YXX'[2...,2...]
				matrix `Xy' = `YXX'[1,2...]
				matrix `b' = `Xy' * syminv(`XX')
				matrix `hat' = `b' * `Xy''
				
				matrix `e' = ``depvar'' - `hat'
				matrix `V' = syminv(`XX') * (`YXX'[1,1] - `hat'[1,1]) / ``dof'' 
				local rows : rownames `V'
				local cols : colnames `V'
				local sevarlist `yvar' `dmxvar' `zvar' `feres'
				tokenize "`sevarlist'"
				matrix accum `BAA' = `sevarlist' if `touse', `constant'
				matrix `AA' = `BAA'[2...,2...]
				matrix `C' = syminv(`AA') * (`YXX'[1,1] - `hat'[1,1]) / ``dof''
				matrix rownames `C' = `rows'
				matrix colnames `C' = `cols'

				replace `touse'= e(sample)
				estimates post `b' `V', depname("`yvar'") obs(``n'') dof (``dof'') esample (`touse')
				/*estimates matrix DM `C'
				matrix `V' = e(DM)
				estimates repost V=`V'*/
 				
				estimates local vcetype fevd
				estimates local depvar `yvar'
				estimates local cmd xtfevd
				est local invariant `zvar'
				est scalar df_fevd = ``dof''
				est scalar df_fe = `dffe'
				est scalar df_m = `dfm'
				}
				
				
				qui{	
					
					tempvar  error errs hats myhat yhat
					predict `yhat'
					gen `error' = `yvar'- `yhat'
					gen `errs' = `error'*`error'
					sum `errs', meanonly
					local mse1 = r(mean)
					local rss1 = r(sum)
					est scalar mse = r(mean)
					est scalar rss= r(sum)
					est scalar rmse = sqrt(r(mean))
					egen `myhat' = mean(`yhat')
					gen `hats' = (`yhat'-`myhat')*(`yhat'-`myhat')
					sum `hats', meanonly
					local ess1 = r(sum)
					est scalar ess= r(sum)
					
					local tss1=`rss1'+`ess1'
					est scalar tss=`rss1'+`ess1'
					est scalar r2 = `ess1' /`tss1'
					est scalar r2_a = 1-((`rss1'/``dof'')/(`tss1'/(``n''-1)))

					

					
					local testvarlist `newxzlist' `feres'
					test `testvarlist'
					
					est scalar F = r(F)
					est scalar F_p = r(p)
				
					}

			
				
				di
				di in gr "panel fixed effects regression with vector decomposition"
				di
				di in gr "degrees of freedom fevd" _col(28) "=" in ye %9.0g e(df_fevd) _col(49) in gr "number of obs" _col(69) "=" in ye %9.0g e(N)
				di in gr "mean squared error" _col(28) "=" in ye %9.0g e(mse)  _col(49) in gr "F( " in ye e(df_m) in gr ", " in ye e(df_fevd) in gr ")" _col(69) "=" in ye %9.0g e(F)
				di in gr "root mean squared error" _col(28) "=" in ye %9.0g e(rmse) _col(49) in gr "Prob > F" _col(69) "=" in ye %9.0g e(F_p)	
				di in gr "Residual Sum of Squares" _col(28) "=" in ye %9.0g e(rss) _col(49) in gr "R-squared" _col(69) "=" in ye %9.0g e(r2)
				di in gr "Total Sum of Squares" _col(28) "=" in ye %9.0g e(tss) _col(49) in gr "adj. R-squared" _col(69) "=" in ye %9.0g e(r2_a)
				di in gr "Estimation Sum of Squares" _col(28) "=" in ye %9.0g e(ess)
				di
				estimates display

		if "`skew'" != "" {   
				di
				di in gr "skewness and normality test for the fixed effects of the first stage and the error term of the second stage
				di
				normtest `fe' `feres'
				}
	
		if "`corr'" != "" {
				di
				di in gr "correlation of fixed effects with explanatory variables"
				di
				corr `fe' `feres' `fullist'
				}
		else { di }
		
	
		}
	

	capture drop `mxvar' `dmxvar' `yvarp' `xzlistp' `fullistp' `praisvarlist' `lvar'




  
end


